[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

cant seem to get system() & pipe to work together

dtown22

10/25/2007 8:59:00 PM

I am trying to run a command line utility which converts a wma file to
a mp3 (i want to be able to do this for batch files) using FFMpeg &
LAME. I am trying to run the following command from ruby:

"C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\ffmpeg.exe" -
i "C:\Documents and Settings\dt\Desktop\my stuff\songs\Who The
\Quadrophenia\07-0 - Who The - Love Reign O'er Me.wma" -vn -f wav - |
"C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\lame.exe" -V
6 - "C:\Documents and Settings\dt\Desktop\my stuff\songs\\Who The
\Quadrophenia\07-0 - Who The - Love Reign O'er Me.mp3"

It works perfect from the command line, but no matter how i try it
(with double quotes, single quotes, backticks, etc) it doesnt seem to
want to work from ruby

Here is an example of the code I have

ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs\wma\\ffmpeg.exe"'
lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs\wma\\lame.exe"'

newFile = child.sub('wma', 'mp3')
#cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
" + newFile
cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
#{newFile}"
puts cmd1
system(cmd1)

NOTE: child points to the same file wma file as above.

If i copy and paste the output of the puts command above into a
command window, it works fine...but from ruby, it seems to ignore the
pipe (|) command. I dont know if this is a windows issue, or if i am
missing something obvious. Any help is greatly appreciated.

10 Answers

Michael Linfield

10/25/2007 11:31:00 PM

0

> ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> \wma\\ffmpeg.exe"'
> lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> \wma\\lame.exe"'
>
> newFile = child.sub('wma', 'mp3')
> #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> " + newFile
> cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> #{newFile}"
> puts cmd1
> system(cmd1)
>
> NOTE: child points to the same file wma file as above.
>
> If i copy and paste the output of the puts command above into a
> command window, it works fine...but from ruby, it seems to ignore the
> pipe (|) command. I dont know if this is a windows issue, or if i am
> missing something obvious. Any help is greatly appreciated.

I have no idea if this was intentional or not but...
#cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> " + newFile

The # in front of cmd comments that entire line out..once again i have
no idea if you were just putting that there to make a point in your post
so I'm just making sure.

- Mac
--
Posted via http://www.ruby-....

dtown22

10/26/2007 4:29:00 PM

0

Hi Mac,

That commented line was a previous attempt at formatting the 'cmd'
differently. I tried many different formats and varations, none of
which seemed to work.

Thanks

-d

On Oct 25, 4:31 pm, Michael Linfield <globyy3...@hotmail.com> wrote:
> > ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> > \wma\\ffmpeg.exe"'
> > lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> > \wma\\lame.exe"'
>
> > newFile = child.sub('wma', 'mp3')
> > #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> > " + newFile
> > cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> > #{newFile}"
> > puts cmd1
> > system(cmd1)
>
> > NOTE: child points to the same file wma file as above.
>
> > If i copy and paste the output of the puts command above into a
> > command window, it works fine...but from ruby, it seems to ignore the
> > pipe (|) command. I dont know if this is a windows issue, or if i am
> > missing something obvious. Any help is greatly appreciated.
>
> I have no idea if this was intentional or not but...
> #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
>
> > " + newFile
>
> The # in front of cmd comments that entire line out..once again i have
> no idea if you were just putting that there to make a point in your post
> so I'm just making sure.
>
> - Mac
> --
> Posted viahttp://www.ruby-....


Michael Linfield

10/26/2007 5:25:00 PM

0

dtown22@gmail.com wrote:
> "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\ffmpeg.exe" -
> i "C:\Documents and Settings\dt\Desktop\my stuff\songs\Who The
> \Quadrophenia\07-0 - Who The - Love Reign O'er Me.wma" -vn -f wav - |
> "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\lame.exe" -V
> 6 - "C:\Documents and Settings\dt\Desktop\my stuff\songs\\Who The
> \Quadrophenia\07-0 - Who The - Love Reign O'er Me.mp3"
>
> It works perfect from the command line, but no matter how i try it
> (with double quotes, single quotes, backticks, etc) it doesnt seem to
> want to work from ruby
>
> Here is an example of the code I have
>
> ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> \wma\\ffmpeg.exe"'
> lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> \wma\\lame.exe"'
>
> newFile = child.sub('wma', 'mp3')
> #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> " + newFile
> cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> #{newFile}"
> puts cmd1
> system(cmd1)
>
> NOTE: child points to the same file wma file as above.
>
> If i copy and paste the output of the puts command above into a
> command window, it works fine...but from ruby, it seems to ignore the
> pipe (|) command. I dont know if this is a windows issue, or if i am
> missing something obvious. Any help is greatly appreciated.

Alright lets approach this from a different perspective. First, the
reason that lame or ffmpeg arnt working is because its loading them as
strings into ruby, instead of loading the exe's. There should be a gem
or lib in ruby that loads exe's into memory but i havnt had the time
today to fumble around the net for it. Using a gem to convert these
files would be alot better than loading exes into memory. If you were on
linux you could do all this with some bash scripting but :( we'll find
another way.

>cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> #{newFile}"

the reason this isnt working is because Ruby doesnt take command line
arguements like Windows cmd.exe or command.com does. Ruby interprets
these as minus signs and negative signs. I would imagine running this
gives you some outrageous errors. If you could post your entire program
and we'll see if we cant find a gem or lib that will do this.

- Mac
--
Posted via http://www.ruby-....

dtown22

10/26/2007 6:35:00 PM

0

Hi Mac,

Thanks for taking the time to help me on this. I wasnt aware that
there was a way to load executables into ruby, but the weird thing, is
that if I remove the pipe (|), or separate the commands, those files
are executed. Which is why I am lead to believe that my problem is the
result of the pipe. But here is my program:

(I slightly modified it to simplify things, but you should get the
idea)

def convert2MP3(path)
raise ArgumentError unless File.directory?(path)

ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs\wma\\ffmpeg.exe"'
lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs\wma\\lame.exe"'

Dir.entries(path).each do |child|
if File.basename(child) =~ /wma$/
puts "converting to mp3"
child = path + File::SEPARATOR + child
child = child.gsub('/', "\\")
child = child.gsub('\\\\', "\\")
child = '"' + child + '"'
newFile = child.sub('wma', 'mp3')
#cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V
6 - " + newFile
cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
#{newFile}"
#cmd2 = `#{lame} -V 6 - #{newFile}`
puts cmd1
system(cmd1)

if File.exists?(child)
#File.delete(child)
end
end
end
end

startingDir = "C:/Documents and Settings/dt/Desktop/my stuff/songs//
Who The/Quadrophenia/"

convert2MP3(startingDir)

-d

On Oct 26, 10:24 am, Michael Linfield <globyy3...@hotmail.com> wrote:
> dtow...@gmail.com wrote:
> > "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\ffmpeg.exe" -
> > i "C:\Documents and Settings\dt\Desktop\my stuff\songs\Who The
> > \Quadrophenia\07-0 - Who The - Love Reign O'er Me.wma" -vn -f wav - |
> > "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\lame.exe" -V
> > 6 - "C:\Documents and Settings\dt\Desktop\my stuff\songs\\Who The
> > \Quadrophenia\07-0 - Who The - Love Reign O'er Me.mp3"
>
> > It works perfect from the command line, but no matter how i try it
> > (with double quotes, single quotes, backticks, etc) it doesnt seem to
> > want to work from ruby
>
> > Here is an example of the code I have
>
> > ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> > \wma\\ffmpeg.exe"'
> > lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> > \wma\\lame.exe"'
>
> > newFile = child.sub('wma', 'mp3')
> > #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> > " + newFile
> > cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> > #{newFile}"
> > puts cmd1
> > system(cmd1)
>
> > NOTE: child points to the same file wma file as above.
>
> > If i copy and paste the output of the puts command above into a
> > command window, it works fine...but from ruby, it seems to ignore the
> > pipe (|) command. I dont know if this is a windows issue, or if i am
> > missing something obvious. Any help is greatly appreciated.
>
> Alright lets approach this from a different perspective. First, the
> reason that lame or ffmpeg arnt working is because its loading them as
> strings into ruby, instead of loading the exe's. There should be a gem
> or lib in ruby that loads exe's into memory but i havnt had the time
> today to fumble around the net for it. Using a gem to convert these
> files would be alot better than loading exes into memory. If you were on
> linux you could do all this with some bash scripting but :( we'll find
> another way.
>
> >cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> > #{newFile}"
>
> the reason this isnt working is because Ruby doesnt take command line
> arguements like Windows cmd.exe or command.com does. Ruby interprets
> these as minus signs and negative signs. I would imagine running this
> gives you some outrageous errors. If you could post your entire program
> and we'll see if we cant find a gem or lib that will do this.
>
> - Mac
> --
> Posted viahttp://www.ruby-....


yermej

10/26/2007 8:40:00 PM

0

On Oct 25, 3:58 pm, "dtow...@gmail.com" <dtow...@gmail.com> wrote:
> I am trying to run a command line utility which converts a wma file to
> a mp3 (i want to be able to do this for batch files) using FFMpeg &
> LAME. I am trying to run the following command from ruby:
>
> "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\ffmpeg.exe" -
> i "C:\Documents and Settings\dt\Desktop\my stuff\songs\Who The
> \Quadrophenia\07-0 - Who The - Love Reign O'er Me.wma" -vn -f wav - |
> "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\lame.exe" -V
> 6 - "C:\Documents and Settings\dt\Desktop\my stuff\songs\\Who The
> \Quadrophenia\07-0 - Who The - Love Reign O'er Me.mp3"
>
> It works perfect from the command line, but no matter how i try it
> (with double quotes, single quotes, backticks, etc) it doesnt seem to
> want to work from ruby
>
> Here is an example of the code I have
>
> ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> \wma\\ffmpeg.exe"'
> lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> \wma\\lame.exe"'
>
> newFile = child.sub('wma', 'mp3')
> #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> " + newFile
> cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> #{newFile}"
> puts cmd1
> system(cmd1)
>
> NOTE: child points to the same file wma file as above.
>
> If i copy and paste the output of the puts command above into a
> command window, it works fine...but from ruby, it seems to ignore the
> pipe (|) command. I dont know if this is a windows issue, or if i am
> missing something obvious. Any help is greatly appreciated.

As far as I can tell, this is due to the way the Windows command
interpreter handles double-quotes when a pipe is present along with
the way Ruby's #system method calls the interpreter.

>From a few tests I did, it seemed the leading double-quote was being
stripped from the command line, but adding another did not fix the
problem. I think you could fix it by either relocating your
executables to a path with no spaces or using a temp file rather than
a pipe. Maybe not optimal, but, like you, I fiddled with all sorts of
single/double quotes and escaping and it didn't like any of it.

dtown22

10/26/2007 10:50:00 PM

0

I don't like the idea of relocating variables, as I would like this
script to be as generic as possible...and I have thought of using a
temporary file, but not a big fan of it either.

None the less, thanks for trying to take a look at it. I don't much
understand why this doesnt work...and i will try to continue to play
around with it and see if i can come up with a combination which
works.

thanks again!

On Oct 26, 1:40 pm, "yer...@gmail.com" <yer...@gmail.com> wrote:
> On Oct 25, 3:58 pm, "dtow...@gmail.com" <dtow...@gmail.com> wrote:
>
>
>
> > I am trying to run a command line utility which converts a wma file to
> > a mp3 (i want to be able to do this for batch files) using FFMpeg &
> > LAME. I am trying to run the following command from ruby:
>
> > "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\ffmpeg.exe" -
> > i "C:\Documents and Settings\dt\Desktop\my stuff\songs\Who The
> > \Quadrophenia\07-0 - Who The - Love Reign O'er Me.wma" -vn -f wav - |
> > "C:\Documents and Settings\dt\Desktop\my stuff\songs\wma\lame.exe" -V
> > 6 - "C:\Documents and Settings\dt\Desktop\my stuff\songs\\Who The
> > \Quadrophenia\07-0 - Who The - Love Reign O'er Me.mp3"
>
> > It works perfect from the command line, but no matter how i try it
> > (with double quotes, single quotes, backticks, etc) it doesnt seem to
> > want to work from ruby
>
> > Here is an example of the code I have
>
> > ffmpeg = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> > \wma\\ffmpeg.exe"'
> > lame = '"C:\\Documents and Settings\\dt\\Desktop\\my stuff\\songs> > \wma\\lame.exe"'
>
> > newFile = child.sub('wma', 'mp3')
> > #cmd = ffmpeg + " -i " + child + " -vn -f wav - | " + lame + " -V 6 -
> > " + newFile
> > cmd1 = "#{ffmpeg} -i #{child} -vn -f wav - | #{lame} -V 6 -
> > #{newFile}"
> > puts cmd1
> > system(cmd1)
>
> > NOTE: child points to the same file wma file as above.
>
> > If i copy and paste the output of the puts command above into a
> > command window, it works fine...but from ruby, it seems to ignore the
> > pipe (|) command. I dont know if this is a windows issue, or if i am
> > missing something obvious. Any help is greatly appreciated.
>
> As far as I can tell, this is due to the way the Windows command
> interpreter handles double-quotes when a pipe is present along with
> the way Ruby's #system method calls the interpreter.
>
> >From a few tests I did, it seemed the leading double-quote was being
>
> stripped from the command line, but adding another did not fix the
> problem. I think you could fix it by either relocating your
> executables to a path with no spaces or using a temp file rather than
> a pipe. Maybe not optimal, but, like you, I fiddled with all sorts of
> single/double quotes and escaping and it didn't like any of it.


Michael Linfield

10/27/2007 1:46:00 AM

0

Alright lets start this program over and build up from the basics using
a gem.


require 'rubygems'
require 'r2mp3'

print "File Path: "
path = gets.chomp

Converter.new(:convert=>:wma,:file=>"#{path}"){|f| f.to_mp3 }

##########

Keep in mind you will have to install the r2mp3 gem via command line -
gem install r2mp3

This SHOULD work, i dont have any resources in front of me right now so
i cant double check, but let me know if you need any additions to the
code above.

- Mac
--
Posted via http://www.ruby-....

dtown22

10/29/2007 4:21:00 PM

0

Hi Mac,

I wasnt aware of a wma to mp3 converter in ruby...i don't have time
today, but tomorrow I will give this a try and let you know how it
goes.

On Oct 26, 6:46 pm, Michael Linfield <globyy3...@hotmail.com> wrote:
> Alright lets start this program over and build up from the basics using
> a gem.
>
> require 'rubygems'
> require 'r2mp3'
>
> print "File Path: "
> path = gets.chomp
>
> Converter.new(:convert=>:wma,:file=>"#{path}"){|f| f.to_mp3 }
>
> ##########
>
> Keep in mind you will have to install the r2mp3 gem via command line -
> gem install r2mp3
>
> This SHOULD work, i dont have any resources in front of me right now so
> i cant double check, but let me know if you need any additions to the
> code above.
>
> - Mac
> --
> Posted viahttp://www.ruby-....


yermej

10/29/2007 5:30:00 PM

0

On Oct 29, 11:21 am, "dtow...@gmail.com" <dtow...@gmail.com> wrote:
> Hi Mac,
>
> I wasnt aware of a wma to mp3 converter in ruby...i don't have time
> today, but tomorrow I will give this a try and let you know how it
> goes.
>
> On Oct 26, 6:46 pm, Michael Linfield <globyy3...@hotmail.com> wrote:
>
> > Alright lets start this program over and build up from the basics using
> > a gem.
>
> > require 'rubygems'
> > require 'r2mp3'
>
> > print "File Path: "
> > path = gets.chomp
>
> > Converter.new(:convert=>:wma,:file=>"#{path}"){|f| f.to_mp3 }
>
> > ##########
>
> > Keep in mind you will have to install the r2mp3 gem via command line -
> > gem install r2mp3
>
> > This SHOULD work, i dont have any resources in front of me right now so
> > i cant double check, but let me know if you need any additions to the
> > code above.
>
> > - Mac
> > --
> > Posted viahttp://www.ruby-....

Just so you know, r2mp3 uses temp files for conversion so if that's an
actual problem rather than a preference, you might need something else.

dtown22

10/31/2007 11:26:00 PM

0

Just as an update, I ended up splitting the commands up into 2
separate commands, and using a temporary file. Its not the ideal
solution, but I havent been able to figure out why the pipe doesnt
want to work (probably a windows thing).

Thanks again for all your help!