mp3 to ogg with Ruby & GStreamer in 50 lines

For this you will need ruby-gstreamer, part of ruby-gnome project.

m2o

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#!/usr/bin/ruby

require 'gst'

if ARGV.size != 2
  puts "Usage: #{$0} mp3-file ogg-file"
  exit
end

# Create gstreamer elements */
pipeline = Gst::Pipeline.new "audio-player"
source  = Gst::ElementFactory.make "filesrc",      "file-source"
decoder = Gst::ElementFactory.make "mad",          "mp3-decoder"
conv    = Gst::ElementFactory.make "audioconvert", "converter"
encoder = Gst::ElementFactory.make "vorbisenc",    "vorbis-encoder"
muxer   = Gst::ElementFactory.make "oggmux",       "ogg-muxer"
sink    = Gst::ElementFactory.make "filesink",     "file-output"

source.location = ARGV[0]
sink.location = ARGV[1]

main_loop = GLib::MainLoop.new

pipeline.bus.add_watch do |bus, message|
  case message.type
  when Gst::Message::EOS
    puts 'End of stream'
    main_loop.quit
  when Gst::Message::ERROR
    puts message.parse.join ': '
    main_loop.quit
  when Gst::Message::INFO
    puts "Info: #{message.parse}"
  end
  true
end

# add elements to pipeline and link
pipeline.add source, decoder, conv, encoder, muxer, sink
source >> decoder >> conv >> encoder >> muxer >> sink

pipeline.play
puts 'Running...'
begin
  main_loop.run
rescue Interrupt
ensure
  puts 'Returned, sopping playing'
  pipeline.stop
end