本文主要是介绍利用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();mediaExtractor.selectTrack(sourceVTrack);MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();videoInfo.presentationTimeUs = 0;long videoSampleTime;{mediaExtractor.readSampleData(inputBuffer, 0);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();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;}mediaExtractor.selectTrack(sourceATrack);MediaCodec.BufferInfo audioInfo = new MediaCodec.BufferInfo();audioInfo.presentationTimeUs = 0;long audioSampleTime;{mediaExtractor.readSampleData(inputBuffer, 0);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;}mediaMuxer.stop();mediaMuxer.release();mediaExtractor.release();mediaExtractor = null;return true;}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
这里要对(1)进行一下解释,如果视频中带了B帧,源视频时间戳会按照播放顺序来打,这样通过读每帧获取到的时间戳就不是一直增长的,MediaMuxer的writeSampleData函数在写每帧的时间戳时如果时间戳不是增长顺序就会报错,帧率也无法通过MediaFormat.KEY_FRAME_RATE来获取,也可以通过查找MP4文件的stsz关键字来获取总帧数,进而算出帧率,来按帧率给每一帧打时间戳,但是stsz一般都在文件的后面而且含有音视频的文件会有两个stsz,如果文件很大,查找起来就会很慢,所以这里我用了个偷懒的办法,用I帧后面两帧的时间戳绝对差值来当做每帧的时间,然后+=这个时间来为写入的帧打时间戳,虽然时间戳不是源视频的顺序了,但并不影响解码。
还有一点就是unselectTrack这个函数,用selectTrack选择轨道后,如果要切换轨道一定要调用unselectTrack函数来释放轨道,不然读的还是之前的轨道。
这篇关于利用MediaExtractor和MediaMuxer实现视频剪切的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!