Attachment @ How to compress voice recordings with Opus file_download
2018-11-27
2018
11-27
«recorder2opus.rb»
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
#!/usr/bin/ruby

# https://wiki.xiph.org/index.php?title=Opus_Recommended_Settings&mobileaction=toggle_view_desktop#Bandwidth_Transition_Thresholds
# 
#                         Rough Bitrate Required (Kb/s)
#   Bandpass Range (Hz)        Mono          Stereo
#                          Voice  Music  Voice   Music
# NarrowBand (3 - 4000)   12      15     ?      ?
# MediumBand (3 - 6000)   15      18-22  ?      ?
# WideBand (3 - 8000)     16-20   22-28  ?      ?
# SuperWideBand (3-12000) 24-28   28-32  ?      ?
# FullBand (3-20000)      28-40   32-64  32-64  64-128

$VERBOSE=nil

require 'shellwords'

puts "USAGE: #{File.basename __FILE__} input.wav [mplayer options]" if ARGV.size == 0

infile   = ARGV.shift
mpl_opts = ARGV.map(&:shellescape).join ' '

unless infile && File.exist?(infile)
  puts "File not found"
  exit
end

$tmp_files = []  # temp files to remove at exit
$mpl_pid   = nil # mplayer PID

def graceful_exit
  $tmp_files.each{|f| File.unlink(f) if File.exists?(f) }
  # kill mplayer if running
  mpl_gpid = Process.getpgid($mpl_pid) rescue -1
  Process.kill("INT", $mpl_pid) if mpl_gpid >= 0
end
Signal.trap('INT') { graceful_exit; exit }
at_exit{ graceful_exit }

f_fifo = "/tmp/#{File.basename __FILE__}.#{Time.now.strftime '%H%M%S%3N'}"
File.mkfifo f_fifo
$tmp_files << f_fifo

$mpl_pid = spawn %Q| mplayer #{mpl_opts} -really-quiet -vo null -vc null -ao pcm:fast:file=#{f_fifo} -slave -input file=/dev/null #{infile.shellescape} |
sleep 1
system %Q| opusenc --bitrate 24 --title #{infile.shellescape} #{f_fifo} #{infile.sub(/wav/i, 'opus').shellescape} |

Process.wait $mpl_pid