1. 程式人生 > >利用MediaExtractor和MediaMuxer實現視訊剪下

利用MediaExtractor和MediaMuxer實現視訊剪下

客戶要在android手機上做個能視訊剪下的app,由於視訊源只是MP4,所以就想到了用MediaExtractor和MediaMuxer來實現功能,直接上程式碼。

public class VideoDecoder {
    private final static String TAG = "VideoDecoder";
    private MediaCodec mediaDecoder;
    private MediaExtractor mediaExtractor;
    private MediaFormat mediaFormat;
    private MediaMuxer mediaMuxer;
    private
String mime = null; public boolean decodeVideo(String url, long clipPoint, long clipDuration) { int videoTrackIndex = -1; int audioTrackIndex = -1; int videoMaxInputSize = 0; int audioMaxInputSize = 0; int sourceVTrack = 0; int sourceATrack = 0; long
videoDuration, audioDuration; //建立分離器 mediaExtractor = new MediaExtractor(); try { //設定檔案路徑 mediaExtractor.setDataSource(url); //建立合成器 mediaMuxer = new MediaMuxer(url.substring(0, url.lastIndexOf(".")) + "_output.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4); } catch
(Exception e) { Log.e(TAG, "error path" + e.getMessage()); } //獲取每個軌道的資訊 for (int i = 0; i < mediaExtractor.getTrackCount(); i++) { try { mediaFormat = mediaExtractor.getTrackFormat(i); mime = mediaFormat.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { sourceVTrack = i; int width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH); int height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); videoMaxInputSize = mediaFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); videoDuration = mediaFormat.getLong(MediaFormat.KEY_DURATION); //檢測剪輯點和剪輯時長是否正確 if (clipPoint >= videoDuration) { Log.e(TAG, "clip point is error!"); return false; } if ((clipDuration != 0) && ((clipDuration + clipPoint) >= videoDuration)) { Log.e(TAG, "clip duration is error!"); return false; } Log.d(TAG, "width and height is " + width + " " + height + ";maxInputSize is " + videoMaxInputSize + ";duration is " + videoDuration ); //向合成器新增視訊軌 videoTrackIndex = mediaMuxer.addTrack(mediaFormat); } else if (mime.startsWith("audio/")) { sourceATrack = i; int sampleRate = mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE); int channelCount = mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT); audioMaxInputSize = mediaFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); audioDuration = mediaFormat.getLong(MediaFormat.KEY_DURATION); Log.d(TAG, "sampleRate is " + sampleRate + ";channelCount is " + channelCount + ";audioMaxInputSize is " + audioMaxInputSize + ";audioDuration is " + audioDuration ); //新增音軌 audioTrackIndex = mediaMuxer.addTrack(mediaFormat); } Log.d(TAG, "file mime is " + mime); } catch (Exception e) { Log.e(TAG, " read error " + e.getMessage()); } } //分配緩衝 ByteBuffer inputBuffer = ByteBuffer.allocate(videoMaxInputSize); //根據官方文件的解釋MediaMuxer的start一定要在addTrack之後 mediaMuxer.start(); //視訊處理部分 mediaExtractor.selectTrack(sourceVTrack); MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo(); videoInfo.presentationTimeUs = 0; long videoSampleTime; //獲取源視訊相鄰幀之間的時間間隔。(1) { mediaExtractor.readSampleData(inputBuffer, 0); //skip first I frame if (mediaExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) mediaExtractor.advance(); mediaExtractor.readSampleData(inputBuffer, 0); long firstVideoPTS = mediaExtractor.getSampleTime(); mediaExtractor.advance(); mediaExtractor.readSampleData(inputBuffer, 0); long SecondVideoPTS = mediaExtractor.getSampleTime(); videoSampleTime = Math.abs(SecondVideoPTS - firstVideoPTS); Log.d(TAG, "videoSampleTime is " + videoSampleTime); } //選擇起點 mediaExtractor.seekTo(clipPoint, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); while (true) { int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { //這裡一定要釋放選擇的軌道,不然另一個軌道就無法選中了 mediaExtractor.unselectTrack(sourceVTrack); break; } int trackIndex = mediaExtractor.getSampleTrackIndex(); //獲取時間戳 long presentationTimeUs = mediaExtractor.getSampleTime(); //獲取幀型別,只能識別是否為I幀 int sampleFlag = mediaExtractor.getSampleFlags(); Log.d(TAG, "trackIndex is " + trackIndex + ";presentationTimeUs is " + presentationTimeUs + ";sampleFlag is " + sampleFlag + ";sampleSize is " + sampleSize); //剪輯時間到了就跳出 if ((clipDuration != 0) && (presentationTimeUs > (clipPoint + clipDuration))) { mediaExtractor.unselectTrack(sourceVTrack); break; } mediaExtractor.advance(); videoInfo.offset = 0; videoInfo.size = sampleSize; videoInfo.flags = sampleFlag; mediaMuxer.writeSampleData(videoTrackIndex, inputBuffer, videoInfo); videoInfo.presentationTimeUs += videoSampleTime;//presentationTimeUs; } //音訊部分 mediaExtractor.selectTrack(sourceATrack); MediaCodec.BufferInfo audioInfo = new MediaCodec.BufferInfo(); audioInfo.presentationTimeUs = 0; long audioSampleTime; //獲取音訊幀時長 { mediaExtractor.readSampleData(inputBuffer, 0); //skip first sample if (mediaExtractor.getSampleTime() == 0) mediaExtractor.advance(); mediaExtractor.readSampleData(inputBuffer, 0); long firstAudioPTS = mediaExtractor.getSampleTime(); mediaExtractor.advance(); mediaExtractor.readSampleData(inputBuffer, 0); long SecondAudioPTS = mediaExtractor.getSampleTime(); audioSampleTime = Math.abs(SecondAudioPTS - firstAudioPTS); Log.d(TAG, "AudioSampleTime is " + audioSampleTime); } mediaExtractor.seekTo(clipPoint, MediaExtractor.SEEK_TO_CLOSEST_SYNC); while (true) { int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { mediaExtractor.unselectTrack(sourceATrack); break; } int trackIndex = mediaExtractor.getSampleTrackIndex(); long presentationTimeUs = mediaExtractor.getSampleTime(); Log.d(TAG, "trackIndex is " + trackIndex + ";presentationTimeUs is " + presentationTimeUs); if ((clipDuration != 0) && (presentationTimeUs > (clipPoint + clipDuration))) { mediaExtractor.unselectTrack(sourceATrack); break; } mediaExtractor.advance(); audioInfo.offset = 0; audioInfo.size = sampleSize; mediaMuxer.writeSampleData(audioTrackIndex, inputBuffer, audioInfo); audioInfo.presentationTimeUs += audioSampleTime;//presentationTimeUs; } //全部寫完後釋放MediaMuxer和MediaExtractor mediaMuxer.stop(); mediaMuxer.release(); mediaExtractor.release(); mediaExtractor = null; return true; } }

這裡要對(1)進行一下解釋,如果視訊中帶了B幀,源視訊時間戳會按照播放順序來打,這樣通過讀每幀獲取到的時間戳就不是一直增長的,MediaMuxer的writeSampleData函式在寫每幀的時間戳時如果時間戳不是增長順序就會報錯,幀率也無法通過MediaFormat.KEY_FRAME_RATE來獲取,也可以通過查詢MP4檔案的stsz關鍵字來獲取總幀數,進而算出幀率,來按幀率給每一幀打時間戳,但是stsz一般都在檔案的後面而且含有音視訊的檔案會有兩個stsz,如果檔案很大,查詢起來就會很慢,所以這裡我用了個偷懶的辦法,用I幀後面兩幀的時間戳絕對差值來當做每幀的時間,然後+=這個時間來為寫入的幀打時間戳,雖然時間戳不是源視訊的順序了,但並不影響解碼。
還有一點就是unselectTrack這個函式,用selectTrack選擇軌道後,如果要切換軌道一定要呼叫unselectTrack函式來釋放軌道,不然讀的還是之前的軌道。