【7种使用FFmpeg分割视频的方法】【转载】

您所在的位置:网站首页 md9020c使用视频 【7种使用FFmpeg分割视频的方法】【转载】

【7种使用FFmpeg分割视频的方法】【转载】

2024-07-10 10:26| 来源: 网络整理| 查看: 265

做视频媒体运营的朋友要分割视频有很多工具可以用,例如:剪映。如果要把视频分割做成批量任务或者需要很多自定义功能,那么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 1fi FILE="$1"SIZELIMIT="$2"FFMPEG_ARGS="$3" # Duration of the source videoDURATION=$(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 farCUR_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 parti=1 # Filename of the next video partNEXTFILENAME="$BASENAME-$i.$EXTENSION" echo "Duration of source video: $DURATION" # Until the duration of all partial videos has reached the duration of the source videowhile [[ $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.05stripaudio=""ratio=1.00th=0.05add=0.00trim=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 usagefi 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 shiftdone 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 $outtimefrom=0i=1 ffmpeg -i $file -vf blackdetect=d=$dur:pic_th=$ratio:pix_th=$th -f null - 2> ffoutblack_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) )> timestampsfor ii in "${!black_start[@]}"; do half=$(bc -l timestampsdone while read -r timestamp; do duration=`bc -l


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3