【发布时间】:2010-11-07 04:31:02
【问题描述】:
我想知道如何使用 FFMpeg 抓取视频的中间帧。我已经编写了 C# 来在某个时间抓取一帧(即在第 3 秒拉一帧)。但我还没有弄清楚如何使用 FFMpeg 命令找到视频的中间部分。
【问题讨论】:
我想知道如何使用 FFMpeg 抓取视频的中间帧。我已经编写了 C# 来在某个时间抓取一帧(即在第 3 秒拉一帧)。但我还没有弄清楚如何使用 FFMpeg 命令找到视频的中间部分。
【问题讨论】:
这可以被简化,但这里有一些旧的 PHP 代码,应该可以解决问题。 (如果它不在您的路径中,请将位置添加到 ffmpeg)
$output = shell_exec("ffmpeg -i {$path}");
preg_match('/Duration: ([0-9]{2}):([0-9]{2}):([^ ,])+/', $output, $matches);
$time = str_replace("Duration: ", "", $matches[0]);
$time_breakdown = explode(":", $time);
$total_seconds = round(($time_breakdown[0]*60*60) + ($time_breakdown[1]*60) + $time_breakdown[2]);
shell_exec("ffmpeg -y -i {$path} -f mjpeg -vframes 1 -ss " . ($total_seconds / 2) . " -s {$w}x{$h} {$output_filename}");
【讨论】:
通过简单的 shell 脚本,您可以使用ffprobe 获得机器可读的持续时间输出,bc 计算半点,ffmpeg 制作框架:
input=input.mp4; ffmpeg -ss "$(bc -l <<< "$(ffprobe -loglevel error -of csv=p=0 -show_entries format=duration "$input")*0.5")" -i "$input" -frames:v 1 half.png
这消除了对 PHP、echo、awk、tr、grep、sed 等的需求。
【讨论】:
eval "$(ffprobe -v error -of flat=s=_ -show_entries format=duration tmp.mp4)"echo $format_duration > tmp.txtecho /2 >> tmp.txt@ 987654334@ format_duration=tr -d '\n' < tmp.txt 如果有人知道在 Windows 上使用 $format_duration 的更优雅的方法,我会很高兴知道。
bc 返回一个不带前导零的数字,例如.987,这会导致错误:Invalid duration specification for ss: .987。 ss 似乎可以使用额外的前导零,所以我在此处添加 0:... -ss 0"$(bc ...。另见:unix.stackexchange.com/questions/197896/…
FFmpeg 帮助您获取视频的帧率和长度,因此您可以将一个乘以另一个并除以 2 以获得中间帧的数量。
即以每秒 15 帧的速度运行 30 秒的视频:30 * 15 = 450 / 2 = 225,这意味着您需要抓取第 225 帧。
【讨论】:
这个 bash 命令就像一个魅力(经过测试):
avconv -i 'in.mpg' -vcodec mjpeg -vframes 1 -an -f rawvideo -s 420x300 -ss avconv -i in.mpg 2>&1 | grep Duration | awk '{print $2}' | tr -d , | awk -F ':' '{print ($3+$2*60+$1*3600)/2}' out.jpg
【讨论】: