トップ «前の日記(2017年06月27日) 最新 次の日記(2017年09月21日)» 編集

Masa's blog

検索キーワード:

2017年09月17日 Script which uses overlay with ffmpeg [長年日記]

_ overlaymovie.sh

Script which uses overlay with ffmpeg.

#! /bin/sh
#
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
	echo "Usage: $0 -m main_movie -s sub_movie -o output_movie [options]"
	echo "put sub_movie on main_movie into output_movie."
        echo ""
	echo "-m MAIN_MOVIE          movie file for background"
	echo "-s SUB_MOVIE           movie file for overlay"
	echo "-o OUTPUT_MOVIE        movie file for output"
	echo "-a AUDIO_INPUT         ms   for main and sub (default)"
	echo "                       m    for main"
	echo "                       s    for sub"
	echo "                       none for no audio"
	echo "-bv VIDEO_BITRATE      bitrate for video (e.g. 2000k)"
	echo "-ba AUDIO_BITRATE      bitrate for audio (e.g. 192k)"
	echo "-vm MAIN_VOLUME        amplitude for main volume (e.g. 1.2)"
	echo "-vs SUB_VOLUME         amplitude for sub volume (e.g. 1.2)"
	echo "-r FRAME_RATE          frame rate (e.g. 29.97)"
	echo "-szm MAIN_SIZE         size for main movie (e.g. 1920x1080)"
	echo "-szs SUB_SIZE          size for sub movie (e.g. 960x540)"
	echo "-stm MAIN_STARTTIME    start time for main movie (e.g. HH:MM:SS[.mmm])"
	echo "-etm MAIN_ENDTIME      endtime for main movie (e.g. HH:MM:SS[.mmm])"
	echo "-elm MAIN_ELAPSTIME    elapstime for main movie (e.g. HH:MM:SS[.mmm])"
	echo "-sts SUB_STARTTIME     start time for sub movie (e.g. HH:MM:SS[.mmm])"
	echo "-ets SUB_ENDTIME       endtime for sub movie (e.g. HH:MM:SS[.mmm])"
	echo "-els SUB_ELAPSTIME     elapstime for sub movie (e.g. HH:MM:SS[.mmm])"
	echo "-p SUB_POSITION        position for sub movie"
        echo "                       t, n   for top"
        echo "                       b, s   for bottom"
        echo "                       r, e   for right"
        echo "                       l, w   for left"
        echo "                       tr, ne for top-right"
        echo "                       tl, nw for top-left"
	echo "                       br, se for bottom-right (default)"
        echo "                       bl, sw for bottom-left"
        echo "                       c      for center"
        exit 0
fi

main_movie=""
sub_movie=""
output_movie=""
audio_input="ms"
video_bitrate="2000k"
audio_bitrate="192k"
main_volume=""
sub_volume=""
frame_rate="3000/1001"
main_size="1920x1080"
sub_size="960x540"
main_starttime="00:00:00"
main_endtime=""
main_elapstime=""
sub_starttime="00:00:00"
sub_endtime=""
sub_elapstime=""
sub_position="br"

while [ $# -ne 0 ]
do
	case $1 in
	-m)
		shift
		main_movie="$1"
		;;
	-s)
		shift
		sub_movie="$1"
		;;
	-o)
		shift
		output_movie="$1"
		;;
	-a)
		shift
		audio_input="$1"
		;;
	-bv)
		shift
		video_bitrate="$1"
		;;
	-ba)
		shift
		audio_bitrate="$1"
		;;
	-vm)
		shift
		vol=`echo 256 \* $1 | bc | sed -e 's/\..*$//'`
		main_volume="-vol ${vol}"
		;;
	-vs)
		shift
		vol=`echo 256 \* $1 | bc | sed -e 's/\..*$//'`
		sub_volume="-vol ${vol}"
		;;
	-r)
		shift
		frame_rate="$1"
		;;
	-szm)
		shift
		main_size="$1"
		;;
	-szs)
		shift
		sub_size="$1"
		;;
	-stm)
		shift
		main_starttime="$1"
		;;
	-etm)
		shift
		main_endtime="-to $1"
		;;
	-elm)
		shift
		main_elapstime="-t $1"
		;;
	-sts)
		shift
		sub_starttime="$1"
		;;
	-ets)
		shift
		sub_endtime="-to $1"
		;;
	-els)
		shift
		sub_elapstime="-t $1"
		;;
	-p)
		shift
		sub_position="$1"
		;;
	esac
	shift
done

main_width=`echo ${main_size} | cut -dx -f1`
main_height=`echo ${main_size} | cut -dx -f2`

sub_width=`echo ${sub_size} | cut -dx -f1`
sub_height=`echo ${sub_size} | cut -dx -f2`

case ${sub_position} in
t|n)
	sub_x=`echo "(${main_width} - ${sub_width}) / 2" | bc`
	sub_y=0
	;;
tr|ne)
	sub_x=`echo "${main_width} - ${sub_width}" | bc`
	sub_y=0
	;;
r|e)
	sub_x=`echo "${main_width} - ${sub_width}" | bc`
	sub_y=`echo "(${main_height} - ${sub_height}) / 2" | bc`
	;;
br|se)
	sub_x=`echo "${main_width} - ${sub_width}" | bc`
	sub_y=`echo "${main_height} - ${sub_height}" | bc`
	;;
b|s)
	sub_x=`echo "(${main_width} - ${sub_width}) / 2" | bc`
	sub_y=`echo "${main_height} - ${sub_height}" | bc`
	;;
bl|sw)
	sub_x=0
	sub_y=`echo "${main_height} - ${sub_height}" | bc`
	;;
l|w)
	sub_x=0
	sub_y=`echo "(${main_height} - ${sub_height}) / 2" | bc`
	;;
tl|nw)
	sub_x=0
	sub_y=0
	;;
c)
	sub_x=`echo "(${main_width} - ${sub_width}) / 2" | bc`
	sub_y=`echo "(${main_height} - ${sub_height}) / 2" | bc`
	;;
*)
	sub_x=`echo "(${main_width} - ${sub_width}) / 2" | bc`
	sub_y=`echo "(${main_height} - ${sub_height}) / 2" | bc`
	;;
esac

ffmpeg -i ${main_movie} -strict -2 -ss ${main_starttime} ${main_endtime} ${main_elapstime} ${main_volume} -vn -y /tmp/overlaymovie.$$.1.tmp.wav.mp4
ffmpeg -i ${sub_movie} -strict -2 -ss ${sub_starttime} ${sub_endtime} ${sub_elapstime} ${sub_volume} -vn -y /tmp/overlaymovie.$$.2.tmp.wav.mp4
case ${audio_input} in
ms)
	ffmpeg -i /tmp/overlaymovie.$$.1.tmp.wav.mp4 -i /tmp/overlaymovie.$$.2.tmp.wav.mp4 -strict -2 -acodec aac -b:a ${audio_bitrate} -filter_complex amix=inputs=2 -y /tmp/overlaymovie.$$.3.tmp.wav.mp4
	;;
m)
	ffmpeg -i /tmp/overlaymovie.$$.1.tmp.wav.mp4 -strict -2 -acodec aac -b:a ${audio_bitrate} -y /tmp/overlaymovie.$$.3.tmp.wav.mp4
	;;
s)
	ffmpeg -i /tmp/overlaymovie.$$.2.tmp.wav.mp4 -strict -2 -acodec aac -b:a ${audio_bitrate} -y /tmp/overlaymovie.$$.3.tmp.wav.mp4
	;;
esac

ffmpeg -i ${main_movie} -strict -2 -ss ${main_starttime} ${main_endtime} ${main_elapstime} -s ${main_size} -an -y /tmp/overlaymovie.$$.4.tmp.mp4
ffmpeg -i ${sub_movie} -strict -2 -ss ${sub_starttime} ${sub_endtime} ${sub_elapstime} -s ${sub_size} -an -y /tmp/overlaymovie.$$.5.tmp.mp4
ffmpeg -i /tmp/overlaymovie.$$.4.tmp.mp4 -i /tmp/overlaymovie.$$.5.tmp.mp4 -strict -2 -vcodec h264 -b:v ${video_bitrate} -r ${frame_rate} -filter_complex "[0:v][1:v]overlay=${sub_x}:${sub_y}" -y /tmp/overlaymovie.$$.6.tmp.mp4

if [ "X${audio_input}" = "Xnone" ]
then
	ffmpeg -i /tmp/overlaymovie.$$.6.tmp.mp4 -strict -2 -vcodec copy -y ${output_movie}
else
	ffmpeg -i /tmp/overlaymovie.$$.6.tmp.mp4 -i /tmp/overlaymovie.$$.3.tmp.wav.mp4 -strict -2 -vcodec copy -acodec copy -y ${output_movie}
fi

rm -f /tmp/overlaymovie.*.*.tmp.*