其他分享
首页 > 其他分享> > 编写Bash脚本实现使用FFmpeg批量合并视频

编写Bash脚本实现使用FFmpeg批量合并视频

作者:互联网

前言

使用FFmpeg能够很方便的合并同分辨率的视频。很多时候我们可能需要批量化合并视频,此时可以通过编写Bash脚本来实现。
在这里插入图片描述

实现

批量合并视频

合并三个文件夹中的视频结果:

result_path1=task1
result_path2=task2
result_path3=task3
concat_path=concat-videos

if [ ! -d "./$concat_path" ]; then
  mkdir ./$concat_path
fi


for i in {0..20}
do
  echo "process: $i"
  ffmpeg -i ./$result_path1/$i/*.mp4 -i ./$result_path2/$i/*.mp4  -i ./$result_path3/$i/*.mp4 -lavfi hstack=3 -y ./$concat_path/concat-video$i.mp4
done

注:hstack为水平合并,vstack为竖直合并。

根据宽高关系合并视频

有时视频是宽大于高,而有时是高大于宽。此时需要根据宽高关系自适应选择hstack或者vstack。

result_path1=task1
result_path2=task2
result_path3=task3

concat_path=concat-videos

if [ ! -d "./$concat_path" ]; then
  mkdir ./$concat_path
fi

function getWidth(){
    width=( $(ffprobe -v error -show_entries stream=width -of csv=p=0  $1))
    echo $width
}

function getHeight(){
    height=( $(ffprobe -v error -show_entries stream=height -of csv=p=0  $1))
    echo $height
}


for i in {0..20}
do
  echo "process: $i"
  widthInfo=$(getWidth ./$result_path1/$i/*.mp4)
  heightInfo=$(getHeight ./$result_path1/$i/*.mp4)
  if [ $widthInfo -gt $heightInfo ]
  then
    #echo $widthInfo ">" $heightInfo
    ffmpeg -i ./$result_path1/$i/*.mp4 -i ./$result_path2/$i/*.mp4 -i ./$result_path3/$i/*.mp4 -qscale 0 -lavfi vstack=3 -y ./$concat_path/concat-$i.mp4
  else
    #echo $widthInfo "<" $heightInfo
    ffmpeg -i ./$result_path1/$i/*.mp4 -i ./$result_path2/$i/*.mp4 -i ./$result_path3/$i/*.mp4 -qscale 0 -lavfi hstack=3 -y ./$concat_path/concat-$i.mp4
  fi
done

版权说明

本文为原创文章,独家发布在blog.csdn.net/TracelessLe。未经个人允许不得转载。如需帮助请email至tracelessle@163.com
在这里插入图片描述

参考资料

[1] 【持续更新】FFmpeg常用命令小结_TracelessLe的专栏-CSDN博客_ffmpeg更新命令
[2] Bash语法中的For Loop_TracelessLe的专栏-CSDN博客_bash for loop
[3] Bash语法中的if else_TracelessLe的专栏-CSDN博客

标签:path1,FFmpeg,批量,echo,mp4,Bash,path,concat,result
来源: https://blog.csdn.net/TracelessLe/article/details/122381319