#!/bin/sh # Converts audio/video files to a different specified container format with ffmpeg # Depends on the youtube-filename-fixer script for cleaining up YouTube filenames usage() { echo "usage: ffmpeg-convert [options] container video_codec audio_codec files" echo "\npositional parameters" echo "\tcontainer\textension of the container that is being converted to" echo "\tvideo_codec\tvideo codec of the output file" echo "\taudio_codec\taudio codec of the output file" echo "\noptions" echo "\t-d\tdelete original files after conversion" echo "\t-h\tshow this help and exit" } stdin() { while read -r infile do outfile="$(echo $infile | rev | cut -d '.' -f 2 | rev)$container" echo "converting $infile -> $outfile..." ffmpeg -nostdin -loglevel 16 -i "$infile" -c:v $video -c:a $audio "$outfile" [ $delete -eq 1 ] && rm -v "$infile" done } cli() { for infile in $@ do outfile="$(echo $infile | rev | cut -d '.' -f 2 | rev)$container" echo "converting $infile -> $outfile..." ffmpeg -nostdin -loglevel 16 -i "$infile" -c:v $video -c:a $audio "$outfile" [ $delete -eq 1 ] && rm -v "$infile" done } options=$(getopt -o 'dhs' -- "$@") eval set -- "$options" delete=0 stdin=0 while true do case $1 in '-h') usage ; exit 0 ;; '-d') delete=1; shift; continue ;; '-s') stdin=1; shift; continue ;; '--') shift; break;; *) echo "internal error"; exit 1 ;; esac done [ $# -ge 3 ] || (usage && exit 1) # Set variables specified on command line container="$1"; shift; video="$1"; shift; audio="$1"; shift; # Prepend a dot to the container name if not specified already [ "${container#.}" = "$container" ] && container=".$container" # If there is only one file argument and it is "-" [ $stdin -eq 1 ] && stdin || cli $@