トップ 最新 追記

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.*

2017年09月21日 Script to merge movies left-right or top-bottom [長年日記]

_ mergemovies.sh

Script to merge two movies into left-right or top-bottom.

#! /bin/sh
#
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
	echo "Usage: $0 -l left_movie -r right_movie -o output_movie [options]"
	echo "       $0 -t top_movie -b bottom_movie -o output_movie [options]"
	echo "Merge movies into output_movie."
        echo ""
	echo "-l LEFT_MOVIE          movie file for left side"
	echo "-r RIGHT_MOVIE         movie file for right side"
	echo "-t TOP_MOVIE           movie file for top side"
	echo "-b BOTTOM_MOVIE        movie file for bottom side"
	echo "-o OUTPUT_MOVIE        movie file for output"
	echo "-a AUDIO_INPUT         lr   for left and right (default)"
	echo "                       l    for left"
	echo "                       r    for right"
	echo "                       tb   for top and bottom (default)"
	echo "                       t    for top"
	echo "                       b    for bottom"
	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 "-vl VOLUME             amplitude for left volume (e.g. 1.2)"
	echo "                       -vr for right"
	echo "                       -vt for top"
	echo "                       -vb for bottom"
	echo "-fr FRAME_RATE         frame rate (e.g. 29.97)"
	echo "-sz SIZE               size for output (e.g. 1920x1080)"
	echo "-stl STARTTIME         start time for left movie (e.g. HH:MM:SS[.mmm])"
	echo "                       -str for right"
	echo "                       -stt for top"
	echo "                       -stb for bottom"
	echo "-etl ENDTIME           endtime for left movie (e.g. HH:MM:SS[.mmm])"
	echo "                       -etr for right"
	echo "                       -ett for top"
	echo "                       -etb for bottom"
	echo "-ell ELAPSTIME         elapstime for left movie (e.g. HH:MM:SS[.mmm])"
	echo "                       -elr for right"
	echo "                       -elt for top"
	echo "                       -elb for bottom"
        exit 0
fi

function preconvert(){
	input=$1; shift
	output=$1; shift
	pw=$1; shift
	ph=$1; shift
	vbitrate=$1; shift
	abitrate=$1; shift
	starttime=$1; shift
	endtime_with_opt=$1; shift
	volume=$1; shift
	framerate=$1

	iwh=`ffmpeg -i ${input} 2>&1 | egrep 'Stream.*Video' | sed -e 's/^.* \([0-9][0-9]*x[0-9][0-9]*\)[^0-9].*$/\1/g'`
	iw=`echo ${iwh} | cut -dx -f1`
	ih=`echo ${iwh} | cut -dx -f2`

	cond=`echo ${iw}*${ph}/${ih}/${pw} | bc`

	if [ ${cond} -gt 0 ]
	then
		sw=${pw}
		sh=`echo ${pw}*${ih}/${iw} | bc`
		padw=0
		padh=`echo "(${ph} - ${sh})/2" | bc`
	else
		sh=${ph}
		sw=`echo ${ph}*${iw}/${ih} | bc`
		padh=0
		padw=`echo "(${pw} - ${sw})/2" | bc`
	fi

	ffmpeg -i ${input} -ss ${starttime} ${endtime_with_opt} -vcodec h264 -b:v ${vbitrate} -r ${framerate} -strict -2 -acodec aac -b:a ${abitrate} -vol ${volume} -filter_complex "[0:v]scale=${sw}:${sh},pad=${pw}:${ph}:${padw}:${padh}" -y ${output}
}

left_movie=""
right_movie=""
top_movie=""
bottom_movie=""
output_movie=""
audio_input=""
video_bitrate="2000k"
audio_bitrate="192k"
left_volume="256"
right_volume="256"
top_volume="256"
bottom_volume="256"
frame_rate="3000/1001"
size="640x360"
left_starttime="00:00:00"
left_endelapstime="-t 24:00:00"
right_starttime="00:00:00"
right_endelapstime="-t 24:00:00"
top_starttime="00:00:00"
top_endelapstime="-t 24:00:00"
bottom_starttime="00:00:00"
bottom_endelapstime="-t 24:00:00"

while [ $# -ne 0 ]
do
	case $1 in
	-l)
		shift
		left_movie="$1"
		;;
	-r)
		shift
		right_movie="$1"
		;;
	-t)
		shift
		top_movie="$1"
		;;
	-b)
		shift
		bottom_movie="$1"
		;;
	-o)
		shift
		output_movie="$1"
		;;
	-a)
		shift
		audio_input="$1"
		;;
	-bv)
		shift
		video_bitrate="$1"
		;;
	-ba)
		shift
		audio_bitrate="$1"
		;;
	-vl)
		shift
		vol=`echo 256 \* $1 | bc | sed -e 's/\..*$//'`
		left_volume="${vol}"
		;;
	-vr)
		shift
		vol=`echo 256 \* $1 | bc | sed -e 's/\..*$//'`
		right_volume="${vol}"
		;;
	-vt)
		shift
		vol=`echo 256 \* $1 | bc | sed -e 's/\..*$//'`
		top_volume="${vol}"
		;;
	-vb)
		shift
		vol=`echo 256 \* $1 | bc | sed -e 's/\..*$//'`
		bottom_volume="${vol}"
		;;
	-fr)
		shift
		frame_rate="$1"
		;;
	-sz)
		shift
		size="$1"
		;;
	-stl)
		shift
		left_starttime="$1"
		;;
	-etl)
		shift
		left_endelapstime="-to $1"
		;;
	-ell)
		shift
		left_endelapstime="-t $1"
		;;
	-str)
		shift
		right_starttime="$1"
		;;
	-etr)
		shift
		right_endelapstime="-to $1"
		;;
	-elr)
		shift
		right_endelapstime="-t $1"
		;;
	-stt)
		shift
		top_starttime="$1"
		;;
	-ett)
		shift
		top_endelapstime="-to $1"
		;;
	-elt)
		shift
		top_endelapstime="-t $1"
		;;
	-stb)
		shift
		bottom_starttime="$1"
		;;
	-etb)
		shift
		bottom_endelapstime="-to $1"
		;;
	-elb)
		shift
		bottom_endelapstime="-t $1"
		;;
	esac
	shift
done

if [ "X${audio_input}" = "X" ]
then
	if [ "X${left_movie}" = "X" ]
	then
		audio_input="tb"
	else
		audio_input="lr"
	fi
fi

pw=`echo ${size} | cut -dx -f1`
ph=`echo ${size} | cut -dx -f2`

if [ "X${left_movie}" = "X" ]
then
	half_ph=`echo ${ph}/2 | bc`
	preconvert ${top_movie} /tmp/mergemovie.$$.1.tmp.mp4 ${pw} ${half_ph} ${video_bitrate} ${audio_bitrate} ${top_starttime} "${top_endelapstime}" ${top_volume} ${frame_rate}
	preconvert ${bottom_movie} /tmp/mergemovie.$$.2.tmp.mp4 ${pw} ${half_ph} ${video_bitrate} ${audio_bitrate} ${bottom_starttime} "${bottom_endelapstime}" ${bottom_volume} ${frame_rate}
else
	half_pw=`echo ${pw}/2 | bc`
	preconvert ${left_movie} /tmp/mergemovie.$$.1.tmp.mp4 ${half_pw} ${ph} ${video_bitrate} ${audio_bitrate} ${left_starttime} "${left_endelapstime}" ${left_volume} ${frame_rate}
	preconvert ${right_movie} /tmp/mergemovie.$$.2.tmp.mp4 ${half_pw} ${ph} ${video_bitrate} ${audio_bitrate} ${right_starttime} "${right_endelapstime}" ${right_volume} ${frame_rate}
fi

ffmpeg -i /tmp/mergemovie.$$.1.tmp.mp4 -strict -2 -acodec copy -vn -y /tmp/mergemovie.$$.3.tmp.wav.mp4
ffmpeg -i /tmp/mergemovie.$$.2.tmp.mp4 -strict -2 -acodec copy -vn -y /tmp/mergemovie.$$.4.tmp.wav.mp4

case ${audio_input} in
lr|tb)
	ffmpeg -i /tmp/mergemovie.$$.3.tmp.wav.mp4 -i /tmp/mergemovie.$$.4.tmp.wav.mp4 -strict -2 -acodec aac -b:a ${audio_bitrate} -filter_complex amix=inputs=2 -y /tmp/mergemovie.$$.5.tmp.wav.mp4
	;;
l|t)
	mv /tmp/mergemovie.$$.3.tmp.wav.mp4 /tmp/mergemovie.$$.5.tmp.wav.mp4
	;;
r|b)
	mv /tmp/mergemovie.$$.4.tmp.wav.mp4 /tmp/mergemovie.$$.5.tmp.wav.mp4
	;;
esac

ffmpeg -i /tmp/mergemovie.$$.1.tmp.mp4 -vcodec copy -strict -2 -an -y /tmp/mergemovie.$$.6.tmp.mp4
ffmpeg -i /tmp/mergemovie.$$.2.tmp.mp4 -vcodec copy -strict -2 -an -y /tmp/mergemovie.$$.7.tmp.mp4
if [ "X${left_movie}" = "X" ]
then
	ffmpeg -i /tmp/mergemovie.$$.6.tmp.mp4 -i /tmp/mergemovie.$$.7.tmp.mp4 -strict -2 -vcodec h264 -b:v ${video_bitrate} -filter_complex "[0:v]pad=iw:ih*2[v1];[v1][1:v]overlay=0:H/2" -y /tmp/mergemovie.$$.8.tmp.mp4
else
	ffmpeg -i /tmp/mergemovie.$$.6.tmp.mp4 -i /tmp/mergemovie.$$.7.tmp.mp4 -strict -2 -vcodec h264 -b:v ${video_bitrate} -filter_complex "[0:v]pad=iw*2:ih[v1];[v1][1:v]overlay=W/2:0" -y /tmp/mergemovie.$$.8.tmp.mp4
fi

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

rm -f /tmp/mergemovie.$$.*.tmp.mp4 /tmp/mergemovie.$$.*.tmp.wav.mp4

2017年09月24日 How to read QRCode by using zbar-0.10 on Slackware-14.2 [長年日記]

_ Build of zbar-0.10 on Slackware-14.2

  • ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h
  • tar xvf zbar-0.10.tar.bz2
  • cd zbar-0.10
  • ./configure && make && make install
  • rm /usr/include/linux/videodev.h

_ qrcode.sh

#! /bin/sh
LD_PRELOAD=/usr/lib64/libv4l/v4l1compat.so zbarcam /dev/video0 |\
while :
do
	read i
	if [ "X$i" = "X" ]
	then
		break
	else
		isUrl=`echo $i | egrep 'https*://'`
		if [ "X${isUrl}" != "X" ]
		then
			url=`echo $i | sed -e 's/^.*http/http/'`
			echo detected URL is ${url}
			/usr/local/firefox/firefox/firefox ${url}
		else
			echo Ignore $i
		fi
	fi
done