#!/bin/bash
#调用ffmpeg串联合并多个视频文件
#主要用以合并ts文件(各视频片段画面尺寸和编码相同的情况),其他情况此命令可能不适用
SCRIPTPATH=$(realpath $0)
display_usage() {
echo -e "$SCRIPTPATH\n"
echo -e "\twarpper 脚本:调用ffmpeg顺序串联合并多个视频文件."
echo -e "\t主要用以合并ts文件(各视频片段画面尺寸和编码相同的情况),其他情况此命令可能不适用"
echo -e "\t底层命令:ffmpeg -f concat -safe 0 -i filelist.txt -c copy Movie_Joined.mp4"
echo -e "\nUsage:\n\tffmpeg-merge [-o SaveFileName] file1 file2 file3 ..."
echo -e "Example:\n\tffmpeg-merge -o savefile.mp4 1.mp4 2.mp4 3.mp4 ..."
}
# if less than two arguments supplied, display usage
if [ $# -lt 1 ]
then
display_usage
exit 1
fi
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $* == "--help") || $* == "-h" ]]
then
display_usage
exit 0
fi
OLD_IFS=$IFS
IFS=$(echo -e "\n")
destFile=""
declare -a inputFile
while [ ! -z "$*" ]
do
if [[ "${1,,}" == "-o" ]]
then
shift
if [ ! -z "$1" ]
then
destFile="$1"
else
echo "-o 参数没有指定要保存的文件名!"
exit 1
fi
elif [ -f "$1" ]
then
#echo "文件:$1"
inputFile=(${inputFile[@]} "$1")
else
echo -e "参数指定的文件 \"$1\" 不存在,请检查!"
exit 1
fi
shift
done
[ -z "$destFile" ] && destFile="JoinedOut_$(date +'%Y%m%d_%H%M').mp4"
listFile="/tmp/ffmpeg-merge-list.txt.$$"
trap "rm -f $listFile" 0
cat /dev/null>$listFile
for inFile in ${inputFile[@]}
do
#echo "inFile: $inFile"
oneFile=$(cygpath -am "$inFile"|awk '{print "file '\''"$0"'\''"}')
#oneFile=$(cygpath -am "$inFile")
#echo "oneFile: $oneFile"
echo $oneFile>>$listFile
done
#执行合并操作
PATH="/v/mediadeps/ffmpeg/bin:/v/mediadeps/rtmpdump:$PATH"
ffmpeg -f concat -safe 0 -i $(cygpath -am "$listFile") -c copy "$destFile"
IFS=$OLD_IFS