android– 如何使用MediaCodec修剪视频

android– 如何使用MediaCodec修剪视频,第1张

概述我正在尝试使用MediaProjectionAPI录制屏幕.我想修剪媒体投影录制的视频.有没有办法在不使用任何第三方依赖的情况下做到这一点?解决方法:经过大量的挖掘,我找到了这个片段/***@paramsrcPaththepathofsourcevideofile.*@paramdstPaththepathofdestination

我正在尝试使用MediaProjection 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;}

编辑:只是为此命名来源.这是来自谷歌的图库应用程序,允许修剪视频,在一个名为“VIDeoUtils”的文件中:
 https://android.googlesource.com/platform/packages/apps/Gallery2/+/634248d/src/com/android/gallery3d/app/VideoUtils.java

缺少的代码是:

private static final String LOGTAG = "VIDeoUtils";private static final int DEFAulT_BUFFER_SIZE = 1 * 1024 * 1024;
总结

以上是内存溢出为你收集整理的android – 如何使用MediaCodec修剪视频全部内容,希望文章能够帮你解决android – 如何使用MediaCodec修剪视频所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097880.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存