做视频媒体运营的朋友要分割视频有很多工具可以用,例如:剪映。如果要把视频分割做成批量任务或者需要很多自定义功能,那么FFmpeg是个不错的选择,FFmpeg是个命令行工具,也可以写程序调用,对技术人员来说使用起来比较灵活,对于非技术人员可能稍有点麻烦。下面介绍7种使用FFmpeg分割视频的方法。
01
将视频分割成帧
ffmpeg -i video.mp4 thumb%04d.jpg -hide_banner
此命令允许您从视频中提取特定帧,这些帧是组成视频的图像文件。例如视频以每秒24帧的速度运行,则意味着在视频播放时,每秒有24张图像显示在屏幕上。此命令可用于将视频分割为帧并提取单个帧。
02
按大小拆分视频
./split-video.sh huge-video.mov 64000000 "-c:v libx264 -crf 23 -c:a copy -vf scale=960:-1"
在命令中,数字64000000表示64MB,这意味着您的视频将被拆分为每个大小为64MB的块。您可以更改该数字以指定大小。
此命令允许您将较大的视频剪切为特定文件大小的较小视频。当您的视频很大,但只需要特定大小的特定部分用于上传或共享时,这个命令就很有用。其中split-video.sh是依赖于ffmpeg的shell脚本,脚本代码如下:
#!/bin/bash
# Short script to split videos by filesize using ffmpeg by LukeLR
if [ $# -ne 3 ]; then
echo 'Illegal number of parameters. Needs 3 parameters:'
echo 'Usage:'
echo './split-video.sh FILE SIZELIMIT "FFMPEG_ARGS'
echo
echo 'Parameters:'
echo ' - FILE: Name of the video file to split'
echo ' - SIZELIMIT: Maximum file size of each part (in bytes)'
echo ' - FFMPEG_ARGS: Additional arguments to pass to each ffmpeg-call'
echo ' (video format and quality options etc.)'
exit 1
fi
FILE="$1"
SIZELIMIT="$2"
FFMPEG_ARGS="$3"
# Duration of the source video
DURATION=$(ffprobe -i "$FILE" -show_entries format=duration -v quiet -of default=noprint_wrappers=1:nokey=1|cut -d. -f1)
# Duration that has been encoded so far
CUR_DURATION=0
# Filename of the source video (without extension)
BASENAME="${FILE%.*}"
# Extension for the video parts
#EXTENSION="${FILE##*.}"
EXTENSION="mp4"
# Number of the current video part
i=1
# Filename of the next video part
NEXTFILENAME="$BASENAME-$i.$EXTENSION"
echo "Duration of source video: $DURATION"
# Until the duration of all partial videos has reached the duration of the source video
while [[ $CUR_DURATION -lt $DURATION ]]; do
# Encode next part
echo ffmpeg -i "$FILE" -ss "$CUR_DURATION" -fs "$SIZELIMIT" $FFMPEG_ARGS "$NEXTFILENAME"
ffmpeg -ss "$CUR_DURATION" -i "$FILE" -fs "$SIZELIMIT" $FFMPEG_ARGS "$NEXTFILENAME"
# Duration of the new part
NEW_DURATION=$(ffprobe -i "$NEXTFILENAME" -show_entries format=duration -v quiet -of default=noprint_wrappers=1:nokey=1|cut -d. -f1)
# Total duration encoded so far
CUR_DURATION=$((CUR_DURATION + NEW_DURATION))
i=$((i + 1))
echo "Duration of $NEXTFILENAME: $NEW_DURATION"
echo "Part No. $i starts at $CUR_DURATION"
NEXTFILENAME="$BASENAME-$i.$EXTENSION"
done
03
将视频分割为相等持续时间的部分
ffmpeg -i "input_video.MTS" -ss 164 -f segment -segment_time 120 -vcodec copy -reset_timestamps 1 -map 0:0 -an output_video%d.MTS
此命令可用于将视频分割为多个持续时间相同的部分。这对于需要或偏好特定视频持续时间的社交媒体网站非常有用。
04
按场景拆分视频
split.sh -d 0.5 -o /tmp/parts -f file.mp4
这个命令检测单个场景并不总是很准确,但可以试试。split.sh 的内容如下:
# Splits video to separate scenes files when full black frames are found in the video
# Inspired by https://gist.github.com/achesco/4dc2ebf13378a0a61fc26c7fe01f539e
# Who got inspired by https://stackoverflow.com/a/38205105
#!/bin/bash
file=""
out="./"
dur=0.05
stripaudio=""
ratio=1.00
th=0.05
add=0.00
trim=0.00
usage () {
echo "Usage: $(basename $0) [[[-o folder] [-d black duration]] | [-h]] -f file.mp4"
echo
echo "Options:"
echo "-f, --file Input file"
echo "-o, --out Outpup files folder path, default"
echo " to current folder"
echo "-d, --dur Duration for black detection in seconds. 0.05 default (practical single frame)"
echo "-r, --ratio ffmpeg pic_th : Set the threshold for considering a picture black. 1.00 default"
echo "-th, --threshold ffmpeg pix_th : Set the threshold for considering a pixel black. 0.00 default."
echo "-t, --trim Substracts to splitting timestamp in seconds. 0 default"
echo "-a, --add Adds to splitting timestamp in seconds. 0 default"
echo "-sa, --strip-audio Strip audio"
echo "-h, --help Display this help message"
echo
echo "Example: split.sh -d 0.5 -o /tmp/parts -f file.mp4"
echo "Splits file.mp4 file to scenes with black frames during more than 0.5 second"
echo "and saves output parts to /tmp/parts folder"
}
if [ "$1" = "" ]; then
usage
fi
while [ "$1" != "" ]; do
case $1 in
-f | --file )
shift
file=$1
;;
-d | --dur )
shift
dur=$1
;;
-r | --ratio )
shift
ratio=$1
;;
-th | --threshold )
shift
th=$1
;;
-o | --out )
shift
out=$1
;;
-t | --trim )
shift
trim=$1
;;
-a | --add )
shift
add=$1
;;
-sa | --strip-audio )
stripaudio="-an"
;;
-h | --help )
usage
exit
;;
* )
usage
exit 1
esac
shift
done
cut_part () {
duration_flag=""
if [[ "$3" != "" ]]; then
duration_flag="-t"
fi
echo "cutting from $1 during $3"
printf -v fileout "$out/%04d_%s" $2 $filename
ffmpeg -y -loglevel error -hide_banner -i $file -ss $1 $stripaudio $duration_flag $3 $fileout < /dev/null
}
filename=`basename $file`
mkdir -p $out
timefrom=0
i=1
ffmpeg -i $file -vf blackdetect=d=$dur:pic_th=$ratio:pix_th=$th -f null - 2> ffout
black_start=( $(grep blackdetect ffout | grep black_start:[0-9.]* -o | grep "[0-9]*(\.[0-9]*)?" -oE) )
black_duration=( $(grep blackdetect ffout | grep black_duration:[0-9.]* -o | grep "[0-9]*(\.[0-9]*)?" -oE) )
> timestamps
for ii in "${!black_start[@]}"; do
half=$(bc -l <<< "${black_duration[$ii]}/2")
middletime=$(bc -l <<< "${black_start[$ii]} + $half")
echo $middletime | LC_ALL=en_US.UTF-8 awk '{printf "%f", $0}' >> timestamps
echo "" >> timestamps
done
while read -r timestamp; do
duration=`bc -l <<< "$timestamp-$timefrom+$add-$trim" | LC_ALL=en_US.UTF-8 awk '{printf "%f", $0}'`
cut_part $timefrom $i $duration
timefrom=`bc -l <<< "$timestamp+$add-$trim" | LC_ALL=en_US.UTF-8 awk '{printf "%f", $0}'`
i=`expr $i + 1`
done < timestamps
if [[ "$timefrom" != 0 ]]; then
cut_part $timefrom $i
fi
另外还有一个python 替代方案——scenedetect。
#安装场景检测所需模块
pip install scenedetect[opencv] -i https://pypi.tuna.tsinghua.edu.cn/simple
#检验是否已安装场景检测所需模块
pip list
click
colorama
numpy
opencv-python
scenedetect 0.5.5
tqdm
使用方式
scenedetect --input
my_video.mp4
--output
my_video_scenes
--stats my_video.stats.csv
time --start 00:05:00 --end 00:06:00
detect-content #对输入视频执行内容检测算法
list-scenes # 打印场景列表并输出到CSV文件
split-video # 使用ffmpeg或mkvMerge分割输入视频
测试
scenedetect --input /Users/zgq/Downloads/video.mp4 --output /Users/zgq/Downloads/my_video_scenes time --start 00:00:00 --end 00:01:00 detect-content split-video
05
按时间或持续时间分割视频
ffmpeg -ss 00:00:00 -t 00:50:00 -i largefile.mp4 -acodec copy \-vcodec copy smallfile.mp4
上面的命令将视频从开始的50分钟分割。你也可以随意改变参数。只需重复该过程,就可以把剩余部分的视频得分割成多个部分。
06
按宽度分割视频
ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0" output.mp4
在上面的例子中,视频将被裁剪为原高度和宽度的一半。要保持相同的高度,只需将ih组件的参数更改为10/10。您还可以通过在上面示例中的引号内使用:keep_aspect=1参数来保持长宽比。命令如下:
ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0:keep_aspect=1" output.mp4
07
FFmpeg水平分割视频
这本质上和上一个是相同的裁剪功能,但不是将其裁剪为更小的宽度,而是保持宽度不变,并将视频的高度降低50%。以下两个命令,一个是不带高宽比例,一个带高宽比例:
ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0" output.mp4
ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0:keep_aspect=1" output.mp4
原文链接: