mp4parser无法在准确的时间内剪切视频

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mp4parser无法在准确的时间内剪切视频相关的知识,希望对你有一定的参考价值。

我原来的视频是10.3秒。我想从秒2.7到秒5.7开始切割

public static void startTrim(@NonNull File src, @NonNull String dst, long startMs, long endMs, @NonNull OnTrimVideoListener callback) throws IOException {
    final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
    final String fileName = "MP4_" + timeStamp + ".mp4";
    final String filePath = dst;

    File file = new File(filePath);
    file.getParentFile().mkdirs();
    Log.d(TAG, "Generated file path " + filePath);
    genVideoUsingMp4Parser(src, file, startMs, endMs, callback);
}


private static void genVideoUsingMp4Parser(@NonNull File src, @NonNull File dst, long startMs, long endMs, @NonNull OnTrimVideoListener callback) throws IOException {
    // NOTE: Switched to using FileDataSourceViaHeapImpl since it does not use memory mapping (VM).
    // Otherwise we get OOM with large movie files.
    Movie movie = MovieCreator.build(new FileDataSourceViaHeapImpl(src.getAbsolutePath()));

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

    double startTime1 = startMs ;  //2.7
    double endTime1 = endMs,       //5.7  

    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.");
            }


            startTime1 = correctTimeToSyncSample(track, startTime1, false);
            endTime1 = correctTimeToSyncSample(track, endTime1, true);


            timeCorrected = true;

        }
    }

    for (Track track : tracks) {
        long currentSample = 0;
        double currentTime = 0;
        double lastTime = -1;
        long startSample1 = -1;
        long endSample1 = -1;

        for (int i = 0; i < track.getSampleDurations().length; i++) {
            long delta = track.getSampleDurations()[i];


            if (currentTime > lastTime && currentTime <= startTime1) {
                // current sample is still before the new starttime
                startSample1 = currentSample;
            }
            if (currentTime > lastTime && currentTime <= endTime1) {
                // current sample is after the new start time and still before the new endtime
                endSample1 = currentSample;
            }
            lastTime = currentTime;
            currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
            currentSample++;
        }
        movie.addTrack(new AppendTrack(new CroppedTrack(track, startSample1, endSample1)));
    }

    dst.getParentFile().mkdirs();

    if (!dst.exists()) {
        dst.createNewFile();
    }

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

    FileOutputStream fos = new FileOutputStream(dst);
    FileChannel fc = fos.getChannel();
    out.writeContainer(fc);

    fc.close();
    fos.close();
    if (callback != null)
        callback.getResult(Uri.parse(dst.toString()));
}

但是在完成方法correctTimeToSyncSample之后,startTime1获得值2.08 ...而endTime1获得值5.18 ...

startTime1 = 2.0830555555555557 endTime1 = 5.182877777777778

  private static double correctTimeToSyncSample(@NonNull Track track, double cutHere, boolean next) {
    double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
    long currentSample = 0;
    double currentTime = 0;
    for (int i = 0; i < track.getSampleDurations().length; i++) {
        long delta = track.getSampleDurations()[i];

        if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {
            // samples always start with 1 but we start with zero therefore +1
            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];
}

视频已成功保存,但未在我想要的确切时间内保存..

任何人都可以帮我这个

答案

视频只能在关键帧处剪切(在mp4中称为同步采样)。关键帧通常每1到10秒。要获得精确的帧,您需要使用像ffmpeg这样的工具进行转码。

另一答案

您可以添加edts / elst / stss / stsh / sdtp框来执行此操作。

  1. 添加edts / elst框以指示媒体时间和片段持续时间,对于您的情况,“elst”框的媒体时间设置为2.7秒的媒体时间,并且片段持续时间设置为3秒,单位为电影的时间尺度。
  2. 当然,您需要添加同步样本框,影子同步样本框和独立和一次性样本框,以指定第一帧(如果它不是关键帧)的依赖性。

合格的mp4播放器将在开始时间帧之前找到相关同步样本,并通过编辑列表解码所有帧,该编辑列表用于解码您的第一个相关帧,但不会在第一帧之前呈现它们你指定了。

以上是关于mp4parser无法在准确的时间内剪切视频的主要内容,如果未能解决你的问题,请参考以下文章

在特定时间准确剪切视频而不会降低质量

在特定时间准确剪切视频而不会降低质量

视频精准剪切总结

音视频转码合成

音视频转码合成

mp4parser库