【发布时间】:2019-06-05 17:04:27
【问题描述】:
我正在尝试使用 bash 和 ffmpeg 制作一个脚本以每隔一段时间对屏幕截图视频进行批处理(例如每秒 1 个屏幕截图) 但问题是 ffmpeg 没有选项可以输出文件名包含其在视频中的时间戳(不是本地计算机时间)的帧。
我找到了一些 hacky 解决方案来做到这一点,但它们都非常有问题、不准确,并且不适用于所有用例(例如具有可变帧速率的视频)
那么有更好的解决方案或工具来完成此类任务吗?
#!/bin/bash
for i in *.$1; do #define video extension
if [ ! -d "$PWD/${i%.*}" ]; then #if directory does not exist yet
mkdir -p "${i%.*}" #create folder based on filename
ffmpeg -i "$i" -vf fps="1/$2" "$PWD/${i%.*}/${i%.*}.%04d.png" #output screenshots
fi
done
编辑: 我根据 Gyan 的回答重新制作了我的脚本
如果有更专业的人可以指出任何错误,请指出。
#!/bin/bash
#./batch_screenshot.sh [video format] [screenshot interval in seconds]
#given video1.mp4, video2.mp4, etc. within a directory, script will output the video screenshots to folders called video1, video2, etc. with the image filename of video1 - HH-MM-SS.png, video2 - HH-MM-SS.png, etc.
orig="$PWD"
for i in *.$1; do
if [ ! -d "$PWD/${i%.*}" ]; then #if directory does not exist yet
mkdir -p "${i%.*}" #create them
ffmpeg -i "$i" -vf select="floor(t*1/$2)-floor(prev_t*1/$2)" -r 1000 -vsync 0 -frame_pts true "$PWD/${i%.*}/%d.png" #output screenshots
cd "$PWD/${i%.*}"
for r in *.png; do #batch rename all the output files
seconds=$(( "${r%.*}" / 1000 )) #convert miliseconds in filename to seconds
timestamp=$(date -d@"$seconds" -u +%H-%M-%S) #convert seconds to hh:mm:ss format
mv "$r" "${i%.*} - $timestamp.png" #rename
done
cd "$orig"
fi
done
【问题讨论】:
-
@Gyan 谢谢,这可以准确地用于可变帧率的视频,对吧?还有一种方法可以将文件名格式化为文件名的 hh:mm:ss 而不是毫秒?或者我应该在之后用 bash 批量重命名它们?
-
是的,源帧率模式无关紧要。 HHMMSS 是不可能的。
-
但是我应该可以使用 bash 批量重命名所有输出文件,将文件名中的毫秒转换为 HHMMSS,对吧? (我稍后会自己尝试)
-
@Gyan 我根据您的回答重新制作了我的脚本。我这样做对吗?
标签: bash video ffmpeg video-processing video-capture