使用 FFMPEG 以毫秒为单位的无损视频修剪

Posted

技术标签:

【中文标题】使用 FFMPEG 以毫秒为单位的无损视频修剪【英文标题】:Lossless video trimming with milliseconds using FFMPEG 【发布时间】:2019-09-27 04:33:25 【问题描述】:

我已经花了几个星期来测试这里的所有答案,以便在 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"

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

【问题讨论】:

你在传递一个更精确的时间戳的秒数:***.com/questions/23171937/… @danfolkes 我们将时间戳传递为 hh:mm:ss.sss 例如 00:00:10.538 我的猜测是您的输入文件没有提供准确的索引。尝试在切割之前重新包装您的输入,例如使用mov 容器和编解码器副本。 @Harry 非常感谢你的想法。我会试一试的。这需要很多时间吗? 重新包装是视频处理中最快的事情。它应该与您的硬盘驱动器或您的网络连接一样快。 【参考方案1】:

在寻找了几天后,我在 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
     */

【讨论】:

以上是关于使用 FFMPEG 以毫秒为单位的无损视频修剪的主要内容,如果未能解决你的问题,请参考以下文章

AVAssetExportSession - 如何从视频持续时间中修剪毫秒

使用 ffmpeg 修剪视频的百分比

三种方法使用FFmpeg截取视频片断

视频文件太大?使用FFmpeg来无损压缩它

FFmpeg C API - 同步视频和音频

ffmpeg无损裁剪合并视频