-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-audio
executable file
·101 lines (81 loc) · 3.19 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/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)
# settings:
# default format:
set format 'mp3' # opus seems to cut off the last second or so of audio sometimes :(
# code:
set -- script "$(path basename (status filename))"
argparse -n "$script" 'h/help' 'v/verbose' 'f/format=' 'd/delete' 'o/output=' 'O/overwrite' '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, flac, mp3, or wav (default: $format)."
echo " -d/--delete - \"Delete\" (trash) the source files after conversion."
echo " -o/--output - Specify output directory."
echo " -O/--overwrite - Overwrite existing files."
exit
end
if set -q _flag_f
set format "$_flag_f"
end
if set -q _flag_overwrite
set -- overwrite_arg -y
else
set -- overwrite_arg -n
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 $overwrite_arg -i '{1}' -b:a 192k -map_metadata 0 "{2}/{1/.}.opus"
else if test "$format" = 'flac'
# compression_level goes from 0 to 12; 5 is default, 12 is max.
set -- command_line ffmpeg $overwrite_arg -i '{1}' -compression_level 12 -map_metadata 0 "{2}/{1/.}.flac"
else if test "$format" = 'mp3'
# q:a means variable bitrate; 0 means V0.
set -- command_line ffmpeg $overwrite_arg -i '{1}' -q:a 0 -map_metadata 0 -id3v2_version 3 "{2}/{1/.}.mp3"
else if test "$format" = 'wav'
set -- command_line ffmpeg $overwrite_arg -i '{1}' "{2}/{1/.}.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 '{1}'
end
if set -q _flag_p
set -a -- command_line ';' echo '{1}' # 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
if set -q _flag_o
set output "$_flag_o"
else
set output (path dirname "$files[1]")
end
set output (string trim --right --chars='/' "$output")
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 --link $command_line ::: $files ::: $output
if set -q _flag_v
echo "$script: Done!" >&2
end