michael@0: #! /bin/sh michael@0: michael@0: # Set variables michael@0: INFILE=$1 michael@0: INAUDI=$2 michael@0: INPREV=$3 michael@0: INPOST=$4 michael@0: INTITLE=$5 michael@0: THREAD=4 michael@0: michael@0: # Following is not needed michael@0: ## Get the number of frames for fading out michael@0: #FRAMES=`ffmpeg -i $INFILE -an -codec:v copy -f null /dev/null 2>&1 | grep 'frame=' | cut -f 2 -d ' '` michael@0: michael@0: # Start log michael@0: #script procfilm-`date +%Y%m%d:%H:%M:%S`.log michael@0: echo "Redirecting command line out to log file `date +%Y%m%d:..._`.log" michael@0: exec > procfilm-`date +%Y%m%d:%H:%M:%S`.log 2>&1 michael@0: michael@0: # Usage michael@0: echo 'This program will produce six output files, one of each format:' michael@0: echo ' 720p and 360p, WEBM (VP8+VORBIS in WEBM)' michael@0: echo ' 720p and 360p, XIPH (THEORA+VORBIS in OGV)' michael@0: echo ' 720p and 360p, MPEG (H.264+AAC in MP4)' michael@0: echo '' michael@0: echo "Usage: $0 ''" michael@0: echo "Example: $0 Myfilm.avi Myaudio.wav Myprev Mypost 'My best film'" michael@0: echo 'Note: Myprev and Mypost must be PNG files (without file extension.)' michael@0: echo 'Note: Titel can be only one word unless you wrap a phrase in quotes.' michael@0: echo '' michael@0: echo 'Warning: Check your preset paths like /usr/share/ffmpeg and $HOME/.ffmpeg' michael@0: echo '' michael@0: echo "Starting with log processing on `date +%Y%m%d\ %H:%M:%S`" michael@0: echo '' michael@0: michael@0: # Create working subdirectories michael@0: for format in webm xiph mpeg; do michael@0: for resol in 360 720; do michael@0: mkdir work$format-$resol; michael@0: done; michael@0: done michael@0: michael@0: # michael@0: # START OF WEBM ENCODING michael@0: # michael@0: michael@0: # 720p Webm (VP8/Vorbis) michael@0: cd ./workwebm-720 || return 1 michael@0: michael@0: # 720p Prepare main film part michael@0: time ffmpeg -v warning -threads $THREAD -i ../$INFILE -an -codec:v libvpx -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vpre 720p -pass 1 -f null /dev/null 2>&1 michael@0: echo 'Finished first pass main film part encoding...' michael@0: echo '' michael@0: # For only watermark (no delogo nor fade) do this: michael@0: # -vf 'movie=../procomwater.png [watermark]; [in][watermark] overlay=24:608 [out]' michael@0: time ffmpeg -v warning -threads $THREAD -i ../$INFILE -an -codec:v libvpx -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vpre 720p -vf '[in] delogo=x=38:y=616:w=254:h=68:band=4:t=4:show=0 [delogo]; movie=../procomwater.png [watermark]; [delogo][watermark] overlay=24:608, fade=t=in:st=0:d=1:alpha=0, fade=t=out:st=298:d=2:alpha=0 [out]' -f webm -pass 2 $INFILE-vidnur720p.webm michael@0: echo 'Finished second pass main film part encoding...' michael@0: echo '' michael@0: michael@0: # 720p Generate front and back matter michael@0: # WARNING: -loop, -t, -r, -vf, -f, or something else causing sync problems! michael@0: ffmpeg -v warning -loop 1 -i ../$INPREV.png -c:v libvpx -t 12.45 -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vpre 720p -vf 'fade=t=in:st=0:d=1.5:alpha=0, fade=t=out:st=11.5:d=0.5:alpha=0' -f webm $INPREV-720p.webm michael@0: echo 'Finished generation of front matter...' michael@0: echo '' michael@0: ffmpeg -v warning -loop 1 -i ../$INPOST.png -c:v libvpx -t 15 -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vpre 720p -vf 'fade=t=in:st=0:d=1:alpha=0' -f webm $INPOST-720p.webm michael@0: echo 'Finished generation of back matter...' michael@0: echo '' michael@0: michael@0: # 720p Concatanate film parts michael@0: mkvmerge --title "$INTITLE" --default-language en -w -o $INFILE-vidcat720p.webm $INPREV-720p.webm + $INFILE-vidnur720p.webm + $INPOST-720p.webm michael@0: echo 'Finished concatanating front, main, and back part files...' michael@0: echo '' michael@0: michael@0: # 720p Join video and audio in film michael@0: ffmpeg -v warning -i $INFILE-vidcat720p.webm -i ../$INAUDI -map 0 -map 1 -strict -2 -codec:a vorbis -aq 20 -ac 2 -codec:v copy -pix_fmt yuv420p -f webm output-720p.webm michael@0: echo "Finished joining video with audio to produce $INFILE-720p.webm" michael@0: echo '' michael@0: echo '' michael@0: michael@0: # 360p Webm (VP8/Vorbis) michael@0: cd ../workwebm-360 || return 1 michael@0: michael@0: # 360p Prepare main film part michael@0: time ffmpeg -v warning -threads $THREAD -i ../$INFILE -an -codec:v libvpx -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vpre 360p -pass 1 -f null /dev/null 2>&1 michael@0: echo 'Finished first pass main film part encoding...' michael@0: echo '' michael@0: time ffmpeg -v warning -threads $THREAD -i ../$INFILE -an -codec:v libvpx -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vpre 360p -vf '[in] delogo=x=38:y=616:w=254:h=68:band=4:t=4:show=0 [delogo]; movie=../procomwater.png [watermark]; [delogo][watermark] overlay=24:608, fade=t=in:st=0:d=1:alpha=0, fade=t=out:st=298:d=2:alpha=0 [out]' -f webm -pass 2 $INFILE-vidnur360p.webm michael@0: echo 'Finished second pass main film part encoding...' michael@0: echo '' michael@0: michael@0: # 360p Generate front and back matter michael@0: # WARNING: -loop, -t, -r, -vf, -f, or something else causing sync problems! michael@0: ffmpeg -v warning -loop 1 -i ../$INPREV.png -c:v libvpx -t 12.45 -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vpre 360p -vf 'fade=t=in:st=0:d=1.5:alpha=0, fade=t=out:st=11.5:d=0.5:alpha=0' -f webm $INPREV-360p.webm michael@0: echo 'Finished generation of front matter...' michael@0: echo '' michael@0: ffmpeg -v warning -loop 1 -i ../$INPOST.png -c:v libvpx -t 15 -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vpre 360p -vf 'fade=t=in:st=0:d=1:alpha=0' -f webm $INPOST-360p.webm michael@0: echo 'Finished generation of back matter...' michael@0: echo '' michael@0: michael@0: # 360p Concatanate film parts michael@0: mkvmerge --title "$INTITLE" --default-language en -w -o $INFILE-vidcat360p.webm $INPREV-360p.webm + $INFILE-vidnur360p.webm + $INPOST-360p.webm michael@0: echo 'Finished concatanating front, main, and back part files...' michael@0: echo '' michael@0: michael@0: # 360p Join video and audio in film michael@0: ffmpeg -v warning -i $INFILE-vidcat360p.webm -i ../$INAUDI -map 0 -map 1 -strict -2 -codec:a vorbis -aq 20 -ac 2 -codec:v copy -pix_fmt yuv420p -f webm output-360p.webm michael@0: echo "Finished joining video with audio to produce $INFILE-360p.webm" michael@0: echo '' michael@0: echo '' michael@0: michael@0: # michael@0: # START OF MPEG4 ENCODING michael@0: # michael@0: michael@0: # 720p MPEG4 (H.264/MP4) michael@0: cd ../workmpeg-720 || return 1 michael@0: michael@0: # 720p Prepare main film part michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libx264 -b:v 2048k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vpre fastfirstpass -pass 1 -f null /dev/null 2>&1 michael@0: echo 'Finished first pass main film part encoding...' michael@0: echo '' michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libx264 -b:v 2048k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vpre hq -vf '[in] delogo=x=38:y=616:w=254:h=68:band=4:t=4:show=0 [delogo]; movie=../procomwater.png [watermark]; [delogo][watermark] overlay=24:608, fade=t=in:st=0:d=1:alpha=0, fade=t=out:st=298:d=2:alpha=0 [out]' -bsf:v h264_mp4toannexb -f mpegts -pass 2 $INFILE-vidnur720p.ts michael@0: echo 'Finished second pass main film part encoding...' michael@0: echo '' michael@0: michael@0: # 720p Generate front and back matter michael@0: # WARNING: -loop, -t, -r, -vf, -f, or something else causing sync problems! michael@0: ffmpeg -v warning -loop 1 -i ../$INPREV.png -c:v libx264 -t 12.45 -b:v 2048k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1.5:alpha=0, fade=t=out:st=11.5:d=0.5:alpha=0' -f mpegts $INPREV-720p.ts michael@0: echo 'Finished generation of front matter...' michael@0: echo '' michael@0: ffmpeg -v warning -loop 1 -i ../$INPOST.png -c:v libx264 -t 15 -b:v 2048k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1:alpha=0' -f mpegts $INPOST-720p.ts michael@0: echo 'Finished generation of back matter...' michael@0: echo '' michael@0: michael@0: # Now prepare for H.264 concatanation michael@0: # http://trac.ffmpeg.org/wiki/How%20to%20concatenate%20%28join,%20merge%29%20media%20files michael@0: michael@0: ## Dont use this because it reencodes when copying is desired michael@0: ##ffmpeg -v warning -i -i $INFILE -i -filter_complex '[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] concat=n=3:v=1:a=1 [v] [a]' -map '[v]' -map '[a]' fullfilm.mp4 michael@0: michael@0: # 720p Concatanate film parts michael@0: # Actually dont need this if we encode H.264 to MPEGTS in previous encoding michael@0: ##ffmpeg -v warning -i $INFILE.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate.ts michael@0: ffmpeg -i "concat:$INPREV-720p.ts|$INFILE-vidnur720p.ts|$INPOST-720p.ts" -c copy -bsf:a aac_adtstoasc -f mp4 $INFILE-vidcat720p.mp4 michael@0: echo 'Finished concatanating front, main, and back part files...' michael@0: echo '' michael@0: michael@0: # 720p Join video and audio in film michael@0: ffmpeg -v warning -i $INFILE-vidcat720p.mp4 -i ../$INAUDI -map 0 -map 1 -strict -2 -codec:a aac -b:a 128k -ac 2 -sample_fmt:1 fltp -codec:v copy -pix_fmt yuv420p -f mp4 output-720p.mp4 michael@0: echo "Finished joining video with audio to produce $INFILE-720p.mp4" michael@0: echo '' michael@0: echo '' michael@0: michael@0: # 360p MPEG4 (H.264/MP4) michael@0: cd ../workmpeg-360 || return 1 michael@0: michael@0: # 360p Prepare main film part michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libx264 -b:v 768k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vpre fastfirstpass -pass 1 -f null /dev/null 2>&1 michael@0: echo 'Finished first pass main film part encoding...' michael@0: echo '' michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libx264 -b:v 768k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vpre hq -vf '[in] delogo=x=38:y=616:w=254:h=68:band=4:t=4:show=0 [delogo]; movie=../procomwater.png [watermark]; [delogo][watermark] overlay=24:608, fade=t=in:st=0:d=1:alpha=0, fade=t=out:st=298:d=2:alpha=0 [out]' -bsf:v h264_mp4toannexb -f mpegts -pass 2 $INFILE-vidnur360p.ts michael@0: echo 'Finished second pass main film part encoding...' michael@0: echo '' michael@0: michael@0: # 360p Generate front and back matter michael@0: # WARNING: -loop, -t, -r, -vf, -f, or something else causing sync problems! michael@0: ffmpeg -v warning -loop 1 -i ../$INPREV.png -c:v libx264 -t 12.45 -b:v 768k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1.5:alpha=0, fade=t=out:st=11.5:d=0.5:alpha=0' -f mpegts $INPREV-360p.ts michael@0: echo 'Finished generation of front matter...' michael@0: echo '' michael@0: ffmpeg -v warning -loop 1 -i ../$INPOST.png -c:v libx264 -t 15 -b:v 768k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1:alpha=0' -f mpegts $INPOST-360p.ts michael@0: echo 'Finished generation of back matter...' michael@0: echo '' michael@0: michael@0: # Now prepare for H.264 concatanation michael@0: # http://trac.ffmpeg.org/wiki/How%20to%20concatenate%20%28join,%20merge%29%20media%20files michael@0: michael@0: ## Dont use this because it reencodes when copying is desired michael@0: ##ffmpeg -v warning -i -i $INFILE -i -filter_complex '[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] concat=n=3:v=1:a=1 [v] [a]' -map '[v]' -map '[a]' fullfilm.mp4 michael@0: michael@0: # 360p Concatanate film parts michael@0: # Actually dont need this if we encode H.264 to MPEGTS in previous encoding michael@0: ##ffmpeg -v warning -i $INFILE.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate.ts michael@0: ffmpeg -i "concat:$INPREV-360p.ts|$INFILE-vidnur360p.ts|$INPOST-360p.ts" -c copy -bsf:a aac_adtstoasc -f mp4 $INFILE-vidcat360p.mp4 michael@0: echo 'Finished concatanating front, main, and back part files...' michael@0: echo '' michael@0: michael@0: # 360p Join video and audio in film michael@0: ffmpeg -v warning -i $INFILE-vidcat360p.mp4 -i ../$INAUDI -map 0 -map 1 -strict -2 -codec:a aac -b:a 128k -ac 2 -sample_fmt:1 fltp -codec:v copy -pix_fmt yuv420p -f mp4 output-360p.mp4 michael@0: echo "Finished joining video with audio to produce $INFILE-360p.mp4" michael@0: echo '' michael@0: echo '' michael@0: michael@0: # michael@0: # START OF XIPH ENCODING michael@0: # michael@0: michael@0: # 720p OGV michael@0: cd ../workxiph-720 || return 1 michael@0: michael@0: # 720p Prepare main film part michael@0: #ffmpeg -i ../$INFILE -codec:v theora -qscale:v 7 -codec:a vorbis -qscale:a 5 output.ogv michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libtheora -b:v 2400k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -f null -pass 1 /dev/null 2>&1 michael@0: echo 'Finished first pass main film part encoding...' michael@0: echo '' michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libtheora -b:v 2400k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vf '[in] delogo=x=38:y=616:w=254:h=68:band=4:t=4:show=0 [delogo]; movie=../procomwater.png [watermark]; [delogo][watermark] overlay=24:608, fade=t=in:st=0:d=1:alpha=0, fade=t=out:st=298:d=2:alpha=0 [out]' -f ogg -pass 2 $INFILE-vidnur720p.ogv michael@0: echo 'Finished second pass main film part encoding...' michael@0: echo '' michael@0: michael@0: # 720p Generate front and back matter michael@0: # WARNING: -loop, -t, -r, -vf, -f, or something else causing sync problems! michael@0: ffmpeg -v warning -loop 1 -i ../$INPREV.png -c:v libtheora -t 12.55 -b:v 2400k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1.5:alpha=0, fade=t=out:st=11.5:d=0.5:alpha=0' -f ogg $INPREV-720p.ogv michael@0: echo 'Finished generation of front matter...' michael@0: echo '' michael@0: ffmpeg -v warning -loop 1 -i ../$INPOST.png -c:v libtheora -t 15 -b:v 2400k -r 25 -s 1280x720 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1:alpha=0' -f ogg $INPOST-720p.ogv michael@0: echo 'Finished generation of back matter...' michael@0: echo '' michael@0: michael@0: # 720p Concatanate film parts michael@0: #oggCat $INFILE-vidcat720p.ogv $INPREV-720p.ogv $INFILE-vidnur720p.ogv $INPOST-720p.ogv michael@0: mkvmerge --title "$INTITLE" --default-language en -o $INFILE-vidcat720p.mkv $INPREV-720p.ogv + $INFILE-vidnur720p.ogv + $INPOST-720p.ogv michael@0: echo 'Finished concatanating front, main, and back part files...' michael@0: echo '' michael@0: michael@0: # 720p Join video and audio in film michael@0: ffmpeg -v warning -i $INFILE-vidcat720p.mkv -i ../$INAUDI -map 0 -map 1 -strict -2 -codec:a vorbis -qscale:a 6 -ac 2 -codec:v copy -pix_fmt yuv420p -f ogg output-720p.ogv michael@0: echo "Finished joining video with audio to produce $INFILE-720p.ogv" michael@0: echo '' michael@0: echo '' michael@0: michael@0: # 360p OGV michael@0: cd ../workxiph-360 || return 1 michael@0: michael@0: # 360p Prepare main film part michael@0: #ffmpeg -i ../$INFILE -codec:v theora -qscale:v 7 -codec:a vorbis -qscale:a 5 output.ogv michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libtheora -b:v 1024k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -f null -pass 1 /dev/null 2>&1 michael@0: echo 'Finished first pass main film part encoding...' michael@0: echo '' michael@0: time ffmpeg -v warning -threads 0 -i ../$INFILE -an -codec:v libtheora -b:v 1024k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vf '[in] delogo=x=38:y=616:w=254:h=68:band=4:t=4:show=0 [delogo]; movie=../procomwater.png [watermark]; [delogo][watermark] overlay=24:608, fade=t=in:st=0:d=1:alpha=0, fade=t=out:st=298:d=2:alpha=0 [out]' -f ogg -pass 2 $INFILE-vidnur360p.ogv michael@0: echo 'Finished second pass main film part encoding...' michael@0: echo '' michael@0: michael@0: # 360p Generate front and back matter michael@0: # WARNING: -loop, -t, -r, -vf, -f, or something else causing sync problems! michael@0: ffmpeg -v warning -loop 1 -i ../$INPREV.png -c:v libtheora -t 12.50 -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1.5:alpha=0, fade=t=out:st=11.5:d=0.5:alpha=0' -f ogg $INPREV-360p.ogv michael@0: echo 'Finished generation of front matter...' michael@0: echo '' michael@0: ffmpeg -v warning -loop 1 -i ../$INPOST.png -c:v libtheora -t 15 -b:v 1024k -r 25 -s 640x360 -aspect 16:9 -pix_fmt yuv420p -vf 'fade=t=in:st=0:d=1:alpha=0' -f ogg $INPOST-360p.ogv michael@0: echo 'Finished generation of back matter...' michael@0: echo '' michael@0: michael@0: # 360p Concatanate film parts michael@0: #oggCat $INFILE-vidcat360p.ogv $INPREV-360p.ogv $INFILE-vidnur360p.ogv $INPOST-360p.ogv michael@0: mkvmerge --title "$INTITLE" --default-language en -o $INFILE-vidcat360p.mkv $INPREV-360p.ogv + $INFILE-vidnur360p.ogv + $INPOST-360p.ogv michael@0: echo 'Finished concatanating front, main, and back part files...' michael@0: echo '' michael@0: michael@0: # 360p Join video and audio in film michael@0: ffmpeg -v warning -i $INFILE-vidcat360p.mkv -i ../$INAUDI -map 0 -map 1 -strict -2 -codec:a vorbis -qscale:a 5 -ac 2 -codec:v copy -pix_fmt yuv420p -f ogg output-360p.ogv michael@0: echo "Finished joining video with audio to produce $INFILE-360p.ogv" michael@0: echo '' michael@0: echo '' michael@0: michael@0: # michael@0: # Closing michael@0: # michael@0: cd .. michael@0: echo 'Processed files: work*-*/output-*.*' michael@0: ls -ld work*-*/output-*.* michael@0: echo '' michael@0: echo 'Tip: If using HTML5 video, then consider:' michael@0: echo "