aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2004-03-22-xmms-ruby-and-m3u.html
blob: 69f5aac1c516b0541f1bbf24547844df85558be3 (plain)
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
---
date: "2004-03-22T12:40:09Z"
title: XMMS-Ruby and M3U
---

<p>
I wrote this little snippet of code in response to an email, and I thought it might be useful for other people.
</p>

<blockquote>
<p>
<i>Basically, I'm trying to write some code that will connect to a running       
XMMS session, grab the playlist and write out a .pls or a .m3u file.          
Unfortunately, while XMMS can do this in a single operation, their API        
doesn't seem to have a simple hook to do this.                               </i>
</p> 
                                                                                
<p>
Unfortunately, there's nothing in the remote API that lets you do that          
in a single call.  But it should be pretty easy to write; just iterate          
over the list and print out the necessary lines for each entry.                 
Something like this:                    
</p>                                        
                                                                                
<pre>
  module Xmms                                                                   
    class Remote                                                                
      #                                                                         
      # get playlist contents as m3u-encoded string                             
      #                                                                         
      def to_m3u                                                                
        "#EXTM3U\n" + playlist.map { |title, file, time|                        
          "#EXTINF:#{time / 1000},#{title}\n#{file}"                            
        }.join("\n")                                                            
      end                                                                       
    end                                                                         
  end
</pre>                                                                           
                                                                                
<p>
Then, to save a playlist, you can just do the following:                        
                                                                                </p>

<pre>
  xmms = Xmms::Remote.new                                                       
  File::open('path/to/output.m3u', 'w') { |out| out.puts xmms.to_m3u }          
                                                                                </pre>

<p>
Hope this helps...                                                              
</p>
</blockquote>

<p>
<b>Update:</b> I tossed this code in a downloadable file, and I added an
<code>Xmms::Remote#save_m3u</code> method for free.  <a
href='/download/xmms-m3u.rb'>Download xmms-m3u.rb</a>.
</p>