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

Discussion in 'Ruby' started by dtown22@gmail.com, Oct 25, 2007.

  1. Guest

    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.
     
    , Oct 25, 2007
    #1
    1. Advertising

  2. > 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-forum.com/.
     
    Michael Linfield, Oct 26, 2007
    #2
    1. Advertising

  3. Guest

    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 <> 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-forum.com/.
     
    , Oct 26, 2007
    #3
  4. 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-forum.com/.
     
    Michael Linfield, Oct 26, 2007
    #4
  5. Guest

    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 <> wrote:
    > 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-forum.com/.
     
    , Oct 26, 2007
    #5
  6. Guest

    On Oct 25, 3:58 pm, "" <> 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.
     
    , Oct 26, 2007
    #6
  7. Guest

    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, "" <> wrote:
    > On Oct 25, 3:58 pm, "" <> 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.
     
    , Oct 26, 2007
    #7
  8. 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-forum.com/.
     
    Michael Linfield, Oct 27, 2007
    #8
  9. Guest

    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 <> 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-forum.com/.
     
    , Oct 29, 2007
    #9
  10. Guest

    On Oct 29, 11:21 am, "" <> 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 <> 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-forum.com/.


    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.
     
    , Oct 29, 2007
    #10
  11. Guest

    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!
     
    , Oct 31, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Schafer
    Replies:
    1
    Views:
    545
    Joe Smith
    Apr 23, 2004
  2. ben
    Replies:
    11
    Views:
    583
    Jonathan N. Little
    Aug 27, 2005
  3. Luke -
    Replies:
    3
    Views:
    307
    sloan
    Oct 23, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    901
    Lionel B
    Mar 1, 2007
  5. Dave Loynd

    DataList and CheckBoxList don't seem to play well together

    Dave Loynd, Jan 28, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    339
    Dave Loynd
    Jan 29, 2004
Loading...

Share This Page