summaryrefslogtreecommitdiff
path: root/.local/bin/ffmpeg-convert
blob: 171a97b24a81cf4223fa6f13b54389ccd506a1d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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 $@