Skip to main content
Search
Search This Blog
Javier Isassi
Development tid-bits. Memos to self. Code. Cool and interesting stuff.
Share
Get link
Facebook
X
Pinterest
Email
Other Apps
Labels
Programming Ruby Zenity
March 23, 2011
Another way to play your media in Linux using Ruby and Zenity.
Ruby Code below.
#!/usr/bin/ruby require 'find' #this ruby script lets you access and play your podcasts or any mp4 file from your usb device mounted #in the directory: media in a Linux system (maybe Ubuntu). Notice i have my Ipod directory hard-code. #You can hard code yours or modify this script to pass the directory as a paramter. #Provecho. Javier Isassi #first extend the String class with a method to strip quotation marks or any character for that matter. class String def strip_str(str) gsub(/^#{str}|#{str}$/, '') end end #the following command reads all my podcasts and puts them in array called pcasts from oldest to newest #powerful ey? pcasts = Dir['/media/Javier\'s Ipod/iPod_Control/Music/*/*.mp4'].sort_by{ |f| File.ctime(f) } #The following routine returns the index in my array contaiing the podcast file location def findPodcast(pcasts,pcast) ix = 0 # the string pcast comes riddled with quotation marks and a newline character at the #so first with strip the quotation all as one-liner pcastClean = pcast.strip_str('"') #let us now search each element for the sub-string pacastClean, here I use regular expressions pcasts.each_with_index do |p, index | if p =~ /#{pcastClean}/ ix = index end end ix end #The following routine creates a string using the pcasts array for use with zenity..more about that below def createString(pcasts, pcastStr) pcasts.each do |p| tokens = p.split(/\//) pcastStr << "\\\"" << tokens.last << "\\\" " end end pcastStr= "" #create the string to use with zenity createString(pcasts, pcastStr) #this is zenity. A program that creates quick and easy GUI menu's to make your life a bit fancier. #Comes standard with most Linux distributions today. pcast=`ans=$(zenity --width=70 --height=500 --list --column \"Podcast\" #{pcastStr} ); echo $ans` #blast the new line at the end of the string pcast.chomp! #Now that zenity let me select the podcast I want to play, I call findPodcast to return the index, I could just #have put the whole string in zenity but looks messy, not withstanding that APPLE makes us all a favor by #mangling the names of the files inside the ipod. ix = findPodcast(pcasts, pcast) #Presto I have a file location. Now I play this file using the program mplayer which also comes with any #Linux distribution. You couls use VLC or whatever media player you fancy. if pcast != "" cmd = `mplayer \"#{pcasts[ix]}\"`
end
Comments
Popular Posts
November 10, 2007
Fedora 8 Review on DELL D620.
June 30, 2009
Pesky c++ interview questions.
Comments