做視頻媒體運營的朋友要分割視頻有很多工具可以用,例如:剪映。如果要把視頻分割做成批量任務(wù)或者需要很多自定義功能,那么FFmpeg是個不錯的選擇,F(xiàn)Fmpeg是個命令行工具,也可以寫程序調(diào)用,對技術(shù)人員來說使用起來比較靈活,對于非技術(shù)人員可能稍有點麻煩。下面介紹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"
在命令中,數(shù)字64000000表示64MB,這意味著您的視頻將被拆分為每個大小為64MB的塊。您可以更改該數(shù)字以指定大小。
此命令允許您將較大的視頻剪切為特定文件大小的較小視頻。當您的視頻很大,但只需要特定大小的特定部分用于上傳或共享時,這個命令就很有用。其中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
將視頻分割為相等持續(xù)時間的部分
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
此命令可用于將視頻分割為多個持續(xù)時間相同的部分。這對于需要或偏好特定視頻持續(xù)時間的社交媒體網(wǎng)站非常有用。
04
按場景拆分視頻
split.sh -d 0.5 -o /tmp/parts -f file.mp4
這個命令檢測單個場景并不總是很準確,但可以試試。split.sh?的內(nèi)容如下:???????
# 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?#對輸入視頻執(zhí)行內(nèi)容檢測算法
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
按時間或持續(xù)時間分割視頻???????
ffmpeg -ss 00:00:00 -t 00:50:00 -i largefile.mp4 -acodec copy \-vcodec copy smallfile.mp4
上面的命令將視頻從開始的50分鐘分割。你也可以隨意改變參數(shù)。只需重復該過程,就可以把剩余部分的視頻得分割成多個部分。
06
按寬度分割視頻
ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0" output.mp4
在上面的例子中,視頻將被裁剪為原高度和寬度的一半。要保持相同的高度,只需將ih組件的參數(shù)更改為10/10。您還可以通過在上面示例中的引號內(nèi)使用:keep_aspect=1參數(shù)來保持長寬比。命令如下:
ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0:keep_aspect=1" output.mp4
07
FFmpeg水平分割視頻
這本質(zhì)上和上一個是相同的裁剪功能,但不是將其裁剪為更小的寬度,而是保持寬度不變,并將視頻的高度降低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
原文鏈接:文章來源:http://www.zghlxwxcb.cn/news/detail-850052.html
7種使用FFmpeg分割視頻的方法?文章來源地址http://www.zghlxwxcb.cn/news/detail-850052.html
到了這里,關(guān)于【7種使用FFmpeg分割視頻的方法】【轉(zhuǎn)載】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!