我正在尝试使用MediaProcotity API记录屏幕。我想修剪媒体投影记录的视频。有没有办法在不使用任何第三方依赖的情况下做到这一点?
大量挖掘后,我发现了这个片段
/**
* @param srcPath the path of source video file.
* @param dstPath the path of destination video file.
* @param startMs starting time in milliseconds for trimming. Set to
* negative if starting from beginning.
* @param endMs end time for trimming in milliseconds. Set to negative if
* no trimming at the end.
* @param useAudio true if keep the audio track from the source.
* @param useVideo true if keep the video track from the source.
* @throws IOException
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void genVideoUsingMuxer(String srcPath, String dstPath,
int startMs, int endMs, boolean useAudio, boolean
useVideo)
throws IOException {
// Set up MediaExtractor to read from the source.
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(srcPath);
int trackCount = extractor.getTrackCount();
// Set up MediaMuxer for the destination.
MediaMuxer muxer;
muxer = new MediaMuxer(dstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
// Set up the tracks and retrieve the max buffer size for selected
// tracks.
HashMap<Integer, Integer> indexMap = new HashMap<>(trackCount);
int bufferSize = -1;
for (int i = 0; i < trackCount; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
boolean selectCurrentTrack = false;
if (mime.startsWith("audio/") && useAudio) {
selectCurrentTrack = true;
} else if (mime.startsWith("video/") && useVideo) {
selectCurrentTrack = true;
}
if (selectCurrentTrack) {
extractor.selectTrack(i);
int dstIndex = muxer.addTrack(format);
indexMap.put(i, dstIndex);
if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
int newSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
bufferSize = newSize > bufferSize ? newSize : bufferSize;
}
}
}
if (bufferSize < 0) {
bufferSize = DEFAULT_BUFFER_SIZE;
}
// Set up the orientation and starting time for extractor.
MediaMetadataRetriever retrieverSrc = new MediaMetadataRetriever();
retrieverSrc.setDataSource(srcPath);
String degreesString = retrieverSrc.extractMetadata(
MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (degreesString != null) {
int degrees = Integer.parseInt(degreesString);
if (degrees >= 0) {
muxer.setOrientationHint(degrees);
}
}
if (startMs > 0) {
extractor.seekTo(startMs * 1000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
}
// Copy the samples from MediaExtractor to MediaMuxer. We will loop
// for copying each sample and stop when we get to the end of the source
// file or exceed the end time of the trimming.
int offset = 0;
int trackIndex = -1;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
try {
muxer.start();
while (true) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readSampleData(dstBuf, offset);
if (bufferInfo.size < 0) {
InstabugSDKLogger.d(TAG, "Saw input EOS.");
bufferInfo.size = 0;
break;
} else {
bufferInfo.presentationTimeUs = extractor.getSampleTime();
if (endMs > 0 && bufferInfo.presentationTimeUs > (endMs * 1000)) {
InstabugSDKLogger.d(TAG, "The current sample is over the trim end time.");
break;
} else {
bufferInfo.flags = extractor.getSampleFlags();
trackIndex = extractor.getSampleTrackIndex();
muxer.writeSampleData(indexMap.get(trackIndex), dstBuf,
bufferInfo);
extractor.advance();
}
}
}
muxer.stop();
//deleting the old file
File file = new File(srcPath);
file.delete();
} catch (IllegalStateException e) {
// Swallow the exception due to malformed source.
InstabugSDKLogger.w(TAG, "The source video file is malformed");
} finally {
muxer.release();
}
return;
}
编辑:仅命名此源。它来自Google的Gallery应用程序,该应用程序允许在称为" Videoutils"的文件中修剪视频: https://android.googlesource.com/platform/packages/apps/gallery2//634248d/src/src/com/android/gallery3d/app/videoutils.java
缺少的代码是:
private static final String LOGTAG = "VideoUtils";
private static final int DEFAULT_BUFFER_SIZE = 1 * 1024 * 1024;
我正在用上述代码修剪。视频文件有3个TrackFormats
-
{track-id = 1,file-format = video/mp4,level = 1024,mime = video/avc,frame-count = 5427,profile = 8,language =,display-width = 810,csd-1 = java.nio.heapbytebuffer [pos = 0 lim = 8 cap = 8],持续时间= 181080900,display-height = 1440,宽度= 810,rotation-degrees = 0,max-Input-size = 874801,框架,框架,框架-rate = 30,高度= 1440,csd-0 = java.nio.nio.heapbytebuffer [pos = 0 lim = 33 cap = 33]}
-
{max-bitrate = 125588,sample-rate = 44100,track-id = 2,file-format = video/mp4,mime = audio/mp4a latm,profile = 2,bitrate = 125588,语言,语言=,aac-profile = 2,持续时间= 181138866,aac-format-adif = 0,channel-count = 2,max-input-size = 65538,csd-0 = java.nio.nio.nio.heapbytebuffer [pos = 0 lim = 2 lim = 2cap = 2]}
-
{text-format-data = java.nio.nio.heapbytebuffer [pos = 0 lim = 56 cap = 56],track-id = 3,file-format = video/mp4,dirationus = 179640000,mime =text/3gpp-tt,语言=,max-input-size = 65536}
所以我可以修剪该视频。它扔了 MPEG4Writer:不支持视频/音频/元数据以外的Track(Text/3GPP-TT) mpeg4Writer:未支撑的Mime'text/3gpp-tt'
,但我只是修剪视频文件。请任何人解决我的问题