-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-audio
executable file
·82 lines (68 loc) · 2.86 KB
/
convert-audio
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env fish
# convert-audio - Convert a set of audio files into a specified audio format, preserving tags.
# Uses GNU Parallel to parallelize the jobs to convert as fast as possible, but
# with the "nice" option set for minimum CPU priority, to minimize the effect on
# other processes on the system.
#
# requirements: ffmpeg, parallel, trash-put (for --delete)
set -- script "$(path basename (status filename))"
argparse -n "$script" 'h/help' 'v/verbose' 'f/format=' 'd/delete' 'p/print' -- $argv
if set -q _flag_h
echo "$script - Convert a set of audio files into a specified audio format, preserving tags."
echo "Usage: $script [arguments] FILES"
echo
echo " -h/--help - Print help and exit."
echo " -v/--verbose - Print debugging output to stderr."
echo " -f/--format - Set output audio format: can be opus (default), flac, or mp3."
echo " -d/--delete - \"Delete\" (trash) the source files after conversion."
# echo " -c/--confirm - Confirm overwriting existing files (not implemented yet)." # FIX
# echo " -o/--output - Set the output filename format." # FIX?
echo " -p/--print - Print output filenames."
exit
end
set format 'mp3' # opus seems to cut off the last second or so of audio sometimes :(
if set -q _flag_f
set format "$_flag_f"
end
if test "$format" = 'opus'
# 192k opus is approximately equivalent to V0 mp3 (but higher audio quality and smaller file size)
set -- command_line ffmpeg -i '{}' -b:a 192k -map_metadata 0 '{.}.opus'
else if test "$format" = 'flac'
# compression_level goes from 0 to 12; 5 is default, 12 is max.
set -- command_line ffmpeg -i '{}' -compression_level 12 -map_metadata 0 '{.}.flac'
else if test "$format" = 'mp3'
# q:a means variable bitrate; 0 means V0.
set -- command_line ffmpeg -i '{}' -q:a 0 -map_metadata 0 -id3v2_version 3 '{.}.mp3'
else if test "$format" = 'wav'
set -- command_line ffmpeg -i '{}' '{.}.wav'
else
echo "$script: Unsupported format \"$format\"." >&2
exit 1
end
if set -q _flag_d
if not command -qs trash-put
echo "$script: Could not find \"trash-put\" binary in \$PATH; --delete option won't work; exiting..." >&2
exit 2
end
set -a -- command_line ';' trash-put '{}'
end
if set -q _flag_p
set -a -- command_line ';' echo '{}' # FIX: doesn't actually work
end
set log_location "/tmp/$script-logs"
set num_args (count $argv)
if test $num_args -lt 1 # no args? user might be making a mistake, so we exit.
echo "$script: No files specified; exiting..."
exit 1
else
set files $argv
end
rm -rf "$log_location"
mkdir -p "$log_location"
if set -q _flag_v
echo -n "$script: Converting to $format, saving logs to $log_location... " >&2
end
parallel --joblog "$log_location/parallel-log.txt" --results "$log_location" --nice 10 $command_line ::: $files
if set -q _flag_v
echo "$script: Done!" >&2
end