首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >借助mp4Parser对视频进行裁剪

借助mp4Parser对视频进行裁剪
EN

Stack Overflow用户
提问于 2014-11-10 18:28:47
回答 2查看 3.8K关注 0票数 2

我正在尝试修剪或剪切特定时间的视频,例如:- Video-1,它是30秒修剪到Video-2 10秒(0.10秒到0.20秒)。我能够做到这一点,并能够播放这个视频,但在视频的结尾,它给出错误的说法- Sorry! cant play this video.

代码语言:javascript
复制
public static void main(String args) throws IOException {

        Movie movie = new MovieCreator()
                .build(new RandomAccessFileIsoBufferWrapperImpl(
                        new File(
                                "/sdcard/Videos11/"+args+".mp4")));

        List<Track> tracks = movie.getTracks();
        movie.setTracks(new LinkedList<Track>());
        // remove all tracks we will create new tracks from the old

        double startTime = 3.000;
        double endTime = 9.000;

        boolean timeCorrected = false;

        // Here we try to find a track that has sync samples. Since we can only
        // start decoding
        // at such a sample we SHOULD make sure that the start of the new
        // fragment is exactly
        // such a frame
        for (Track track : tracks) {
            if (track.getSyncSamples() != null
                    && track.getSyncSamples().length > 0) {
                if (timeCorrected) {
                    // This exception here could be a false positive in case we
                    // have multiple tracks
                    // with sync samples at exactly the same positions. E.g. a
                    // single movie containing
                    // multiple qualities of the same video (Microsoft Smooth
                    // Streaming file)

                    throw new RuntimeException(
                            "The startTime has already been corrected by another track with SyncSample. Not Supported.");

            }
        }

        for (Track track : tracks) {
            long currentSample = 0;
            double currentTime = 0;
            long startSample = -1;
            long endSample = -1;

            for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) {
                TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i);
                for (int j = 0; j < entry.getCount(); j++) {
                    // entry.getDelta() is the amount of time the current sample
                    // covers.

                    if (currentTime <= startTime) {
                        // current sample is still before the new starttime
                        startSample = currentSample;
                    }
                    if (currentTime <= endTime) {
                        // current sample is after the new start time and still
                        // before the new endtime
                        endSample = currentSample;
                    } else {
                        // current sample is after the end of the cropped video
                        break;
                    }
                    currentTime += (double) entry.getDelta()
                            / (double) track.getTrackMetaData().getTimescale();
                    currentSample++;
                }
            }
            movie.addTrack(new CroppedTrack(track, startSample, endSample));
        }

        IsoFile out = new DefaultMp4Builder().build(movie);

        String filePath = "sdcard/test"+i+".mp4";
        i++;
        File f = new File(filePath);
        FileOutputStream fos = new FileOutputStream(f);
        BufferedOutputStream bos = new BufferedOutputStream(fos, 65535);
        out.getBox(new IsoOutputStream(bos));
        bos.close();
        fos.close();

    }

附言:-我不太熟悉这个代码,但一些如何能够裁剪视频,但它在最后显示错误。

EN

回答 2

Stack Overflow用户

发布于 2016-03-26 19:36:56

你应该“标准化”你的开始和停止值,使它们与mp4同步样本相对应,否则你会在输出视频甚至是损坏的文件上得到一些毛刺。

请看一下correctTimeToSyncSample方法:

  1. Android Gallery source (使用mp4parser 0.9.x,I guess)
  2. My sample project (使用mp4parser 1.1.18)。

代码语言:javascript
复制
private static double correctTimeToSyncSample(Track track, double cutHere, boolean next) {
    double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
    long currentSample = 0;
    double currentTime = 0;
    for (int i = 0; i = 0) {
            timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;
        }
        currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
        currentSample++;
    }
    double previous = 0;
    for (double timeOfSyncSample : timeOfSyncSamples) {
        if (timeOfSyncSample > cutHere) {
            if (next) {
                return timeOfSyncSample;
            } else {
                return previous;
            }
        }
        previous = timeOfSyncSample;
    }
    return timeOfSyncSamples[timeOfSyncSamples.length - 1];
}

用法:

代码语言:javascript
复制
for (Track track : tracks) {
    if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {
        if (timeCorrected) {
            throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
        }
        startTime = correctTimeToSyncSample(track, startTime, false);
        endTime = correctTimeToSyncSample(track, endTime, true);
        timeCorrected = true;
    }
}
票数 2
EN

Stack Overflow用户

发布于 2015-03-24 12:40:29

也许你想要剪切的位置在Iframe的中间。我也面临着类似的问题。我不能准确地剪切我想要的地方,因为在视频的开头我得到了糟糕的帧。你是不是在裁剪过的视频一开始就得到了坏帧?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26841622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档