本文主要是介绍android.MediaMuxer时间裁剪,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用MediaMuxer裁剪视频_安卓muxer 裁剪视频画布-CSDN博客
关键步骤
mediaExtractor.seekTo(beginTime, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);long presentTimeUs = mediaExtractor.getSampleTime();
if (presentTimeUs > endTime)break;
功能代码
VideoView videoView_video;
ImageButton imageButton_startTime, imageButton_endTime;
Button button_clip;
String path_video="";
int startTime, endTime;imageButton_startTime = (ImageButton)findViewById(R.id.imageButton_startTime);
imageButton_startTime.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {startTime = videoView_video.getCurrentPosition(); //msToast.makeText(getApplicationContext(), "开始时间:" + startTime, Toast.LENGTH_SHORT).show();}
});imageButton_endTime = (ImageButton)findViewById(R.id.imageButton_endTime);
imageButton_endTime.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {endTime = videoView_video.getCurrentPosition();Toast.makeText(getApplicationContext(), "结束时间:" + endTime, Toast.LENGTH_SHORT).show();}
});button_clip = (Button)findViewById(R.id.button_clip);
button_clip.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (path_video.equals("")) {Toast.makeText(getApplicationContext(), "错误:没有视频", Toast.LENGTH_SHORT).show();return;}if (startTime >= endTime) {Toast.makeText(getApplicationContext(), "错误:开始时间(" + startTime + ") > 结束时间(" + endTime + ")", Toast.LENGTH_SHORT).show();return;}String path_output = Environment.getExternalStorageDirectory().getPath() + File.separator + Environment.DIRECTORY_MOVIES + path_video.substring(path_video.lastIndexOf("/"), path_video.lastIndexOf(".")) + "_clip.mp4";clipVideo(path_video, path_output, startTime, endTime);}
});
void clipVideo(String path_video, String path_output, int startTime, int endTime) {MediaExtractor mediaExtractor = new MediaExtractor();try {mediaExtractor.setDataSource(path_video);} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());}int videoTrack = -1,audioTrack = -1;if (mediaExtractor.getTrackCount() == 0) {Toast.makeText(getApplicationContext(), "错误:trackCount == 0", Toast.LENGTH_SHORT).show();return;}for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {if (mediaExtractor.getTrackFormat(i).getString(MediaFormat.KEY_MIME).startsWith("video/")) {videoTrack = i;} else if (mediaExtractor.getTrackFormat(i).getString(MediaFormat.KEY_MIME).startsWith("audio/")) {audioTrack = i;}}Toast.makeText(getApplicationContext(), "videoTrack: " + videoTrack + "\naudioTrack: " + audioTrack, Toast.LENGTH_SHORT).show();Log.e(Thread.currentThread().getStackTrace()[2] + "", "videoTrack: " + videoTrack + ", audioTrack: " + audioTrack);MediaFormat videoFormat = mediaExtractor.getTrackFormat(videoTrack);MediaFormat audioFormat = mediaExtractor.getTrackFormat(audioTrack);MediaMuxer mediaMuxer = null;try {mediaMuxer = new MediaMuxer(path_output, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());}int videoTrackIndex = mediaMuxer.addTrack(videoFormat);//int rotation = videoFormat.getInteger(MediaFormat.KEY_ROTATION); //视频的rotation//mediaMuxer.setOrientationHint(rotation);int audioTrackIndex = mediaMuxer.addTrack(audioFormat);mediaMuxer.start();int bufferSize = 256 * 1024;ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);//视频部分mediaExtractor.selectTrack(videoTrack);MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();mediaExtractor.seekTo(startTime * 1000L, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);while (true) {int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0);long presentationTimeUs = mediaExtractor.getSampleTime();if (sampleSize < 0 || presentationTimeUs > endTime * 1000L) {mediaExtractor.unselectTrack(videoTrack);break;}//int trackIndex = mediaExtractor.getSampleTrackIndex();int sampleFlag = mediaExtractor.getSampleFlags();videoInfo.offset = 0;videoInfo.size = sampleSize;videoInfo.flags = sampleFlag;videoInfo.presentationTimeUs = presentationTimeUs;mediaMuxer.writeSampleData(videoTrackIndex, inputBuffer, videoInfo);mediaExtractor.advance();}//音频mediaExtractor.selectTrack(audioTrack);MediaCodec.BufferInfo audioInfo = new MediaCodec.BufferInfo();mediaExtractor.seekTo(startTime * 1000L, MediaExtractor.SEEK_TO_CLOSEST_SYNC);while (true) {int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0);long presentationTimeUs = mediaExtractor.getSampleTime();if (sampleSize < 0 || presentationTimeUs > endTime * 1000L) {mediaExtractor.unselectTrack(audioTrack);break;}//int trackIndex = mediaExtractor.getSampleTrackIndex();int sampleFlag = mediaExtractor.getSampleFlags();audioInfo.offset = 0;audioInfo.size = sampleSize;audioInfo.flags = sampleFlag;audioInfo.presentationTimeUs = presentationTimeUs;mediaMuxer.writeSampleData(audioTrackIndex, inputBuffer, audioInfo);mediaExtractor.advance();}mediaMuxer.stop();mediaMuxer.release();mediaExtractor.release();MediaScannerConnection.scanFile(getApplicationContext(), new String[]{path_output}, null, null);Toast.makeText(getApplicationContext(), "视频剪辑完成:" + path_output, Toast.LENGTH_SHORT).show();
}
源码:GitLink | 确实开源
这篇关于android.MediaMuxer时间裁剪的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!