publicvoid convert() throws IOException{String videoPath ="/sdcard/input.mp4";// Get the video resolutionMediaMetadataRetriever retriever =newMediaMetadataRetriever();int videoResolution =Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));// If the video resolution is higher than 360p, downscale it to 360pif(videoResolution >360){MediaMetadataRetriever retriever =newMediaMetadataRetriever();
retriever.setDataSource(videoPath);Bitmap bmp = retriever.getFrameAtTime();int newWidth =360;int newHeight =(int)(bmp.getHeight()* newWidth / bmp.getWidth());Bitmap resizedBmp =Bitmap.createScaledBitmap(bmp, newWidth, newHeight,true);
retriever.release();// Save the video to the phoneMediaMuxer muxer = null;try{
muxer =newMediaMuxer(videoPath,MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);// Add the video trackint videoTrackIndex = muxer.addTrack(MediaFormat.createVideoFormat(videoPath, newWidth, newHeight));// Add the audio trackint audioTrackIndex = muxer.addTrack(MediaFormat.createAudioFormat(videoPath,44100,2));// Start writing to the muxer
muxer.start();// Write the video frames to the muxerByteBuffer buffer =ByteBuffer.allocateDirect(1024*1024);for(int i =0; i < resizedBmp.getHeight(); i++){
buffer.clear();
resizedBmp.getPixels(buffer.array(),0, resizedBmp.getWidth(),0, i, resizedBmp.getWidth(), resizedBmp.getHeight());
byte[] bytes =ByteBuffer.wrap(buffer.array()).asByteArray();
muxer.writeSampleData(videoTrackIndex, bytes, bytes.length);}// Write the audio samples to the muxerfor(int i =0; i < resizedBmp.getHeight(); i++){
buffer.clear();
resizedBmp.getPixels(buffer.array(),0, resizedBmp.getWidth(),0, i, resizedBmp.getWidth(), resizedBmp.getHeight());
byte[] bytes =ByteBuffer.wrap(buffer.array()).asByteArray();
muxer.writeSampleData(audioTrackIndex, bytes, bytes.length);}// Stop writing to the muxer
muxer.stop();
muxer.release();}catch(IOException e){Log.e("MainActivity","Error writing to muxer", e);}}// Set the video view's media controller
vedgetr.setMediaController(newMediaController(this));// Set the video view's video path
vedgetr.setVideoPath(videoPath);// Start playing the video
vedgetr.start();}
السؤال
Drive Man
1 جواب على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.