-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany2mp3.sf
105 lines (77 loc) · 2.31 KB
/
any2mp3.sf
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
102
103
104
105
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 17 June 2014
# Edit: 21 March 2020
# Website: https://github.com/trizen
# Convert any media file to MP3, using `n` parallel processes of ffmpeg.
func usage(code=0) {
var name = File(__FILE__).basename
print <<"USAGE"
usage:
#{name} [options] [input files]
options:
-b --bitrate=i : MP3 bitrate (default: 192K)
-t --threads=i : use this many threads (default: 2)
-o --outdir=s : output directory (default: .)
example:
#{name} -t 4 *.mp4 # convert MP4 to MP3
USAGE
Sys.exit(code)
}
func executeCmd(cmd, arg) {
Sys.run(cmd, arg...)
}
func wait_th(forks) {
say ":: Running threads: #{forks.len}"
say ":: FFmpeg exit-code: #{forks.pop_rand.wait}"
}
func main() {
const extensionRe = %r/\.\w{1,5}\z/
const outputFormat = "mp3"
const ffmpegCmd = "ffmpeg"
var bitrate = 192
var maxThreads = 2
var outputDir = Dir.cwd
ARGV.getopt!(
'b|bitrate=i' => \bitrate,
'o|outdir=s' => \outputDir,
't|threads=i' => \maxThreads,
'h|help' => usage,
)
var ffmpegArg = ["-loglevel", "fatal", "-y", "-vn", "-ac", "2", "-ab", "#{bitrate}K", "-ar", "48000", "-f", outputFormat]
if (ARGV.len == 0) {
Sys.warn("\n[!] No input file has been provided!\n")
usage(2)
}
if (!outputDir.exists) {
outputDir.create || die "Can't create dir `#{outputDir}': #{$!}"
}
var forks = []
var counter = 0
ARGV.each { |entry|
var file = File(entry)
if (!file.exists) {
Sys.warn("File `#{file}' does not exists! Skipping file...\n")
next
}
if (!file.is_file) {
Sys.warn("File `#{file}' is not a plain file! Skipping it...\n")
next
}
var outputFile = file.basename.sub(extensionRe)
outputFile = File(outputDir, outputFile + '.' + outputFormat)
printf("[%2d] %s -> %s\n", counter++, file, outputFile)
var args = ['-i', file.to_s, ffmpegArg..., outputFile]
forks.append(executeCmd.ffork(ffmpegCmd, args))
if (forks.len >= maxThreads) {
wait_th(forks)
}
}
while (forks.len > 0) {
wait_th(forks)
}
if (counter > 0) {
say ":: All done!"
}
}
main()