首页 > 解决方案 > 使用 FFMPEG 以毫秒为单位的无损视频修剪

问题描述

我已经花了几周的时间来测试这里的所有答案,以便在 Android 上使用 FFMPEG 进行无损切割。无论我尝试什么,我总是会失败。在我们正在构建的应用程序中,我们需要精确到毫秒。

我已经尝试了很多命令的变体,其中很多变体超过半秒。这适用于复制和重新编码。有了这个,我在这两个位置之前和之后都尝试了 -ss,但我没有看到任何区别。这些是效果最好的两个命令,但仍然不够接近:

//copy
"-ss $startTime -t $endTime -i $input -f segment -c copy $output"

//encode
"-i $input -ss $startTime -c:v libx264 -crf 12 -preset ultrafast -t $endTime $output"

有没有人使用过给出更准确结果的命令甚至其他库?

标签: androidvideotrimandroid-ffmpeglossless

解决方案


在寻找了几天后,我在 FFMPEG 中最接近毫秒精确修剪的是使用以下命令:

"-ss $startCutTime -i $inputFilePath -ss 0 -c copy -to $endCutTime -avoid_negative_ts make_zero $outputFilePath"

对于 30fps 剪辑,此命令精确到大约 20 毫秒,但这完全取决于视频的帧速率。我希望这可以帮助其他人。以下是每个组件的作用的解释:

/**
     * FFMPEG Cmd Structure
     * -i: Input file
     * -ss: If this is placed before the input file it seeks to this position in the input file. If it's after the input it will split from that exact position.
     * -to: The position of where the command is going to cut to.
     * -c copy: This copies the video and audio codecs as they are for speed.
     * -avoid_negative_ts make_zero: Sets the first timestamp as 0
     * The times are passed in parsed eg 00:00:10.456
     * The output file is the last part of the command
     */

推荐阅读