Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Passing an in memory scalar to an external command as an argument

by Rodster001 (Pilgrim)
on Apr 08, 2015 at 18:29 UTC ( [id://1122829]=perlquestion: print w/replies, xml ) Need Help??

Rodster001 has asked for the wisdom of the Perl Monks concerning the following question:

Forgive me if this is been asked before, I cannot find the answer and can't quite figure it out myself.

I have an in memory scalar and I want to pass that to an external program in a system command as an argument (instead of first writing a file and then calling the command).

For example:

my $mp3 = <binary contents of an mp3 file already in memory>; system("/path/ffmpeg -i <want to pass $mp3 here>");
I know ffmpeg can take an input argument like this (if this helps):
cat my.mp3 | ffmpeg -i pipe:0
Thanks for the help

Replies are listed 'Best First'.
Re: Passing an in memory scalar to an external command as an argument
by pme (Monsignor) on Apr 08, 2015 at 18:54 UTC
    This may help:
    my $mp3 = <binary contents of an mp3 file already in memory>; $pid = open(PLAY, "| ffmpeg -i pipe:0") or die "Couldn't fork: $!\n"; print PLAY $mp3; close(PLAY) or die "Couldn't close: $!\n";
        Thank you, BINMODE added to fh PLAY

      I would suggest:

      use IO::Pipe; my $pipe = IO::Pipe->new(); $pipe->writer(qw(ffmpeg -i pipe:0)); print $pipe $mp3;

      to avoid the shell being used to launch ffmpeg.

        I like that solution. But I need to capture stdout also (so I am actually using open2 for this). Can I write and read using IO::Pipe? The docs look like it is one or the other.
      That's what I was looking for, thank you sir
Re: Passing an in memory scalar to an external command as an argument
by Marshall (Canon) on Apr 09, 2015 at 03:47 UTC
    In Perl, you can open an internal Perl variable with a file handle to read it like a file. Instead of a file name, you use a reference to the internal Perl variable name. You can also use this technique to print to an internal Perl variable. This is often a good technique to avoid the complications of creating temporary files. If the program "bombs", there are no extra files to clean up later.

    open (MP3, '<' , \$mp3file) or die $!; binmode MP3; #more than one way to do this print MP3;
    yourprogram.pl > binaryfileconsumer;
Re: Passing an in memory scalar to an external command as an argument
by Laurent_R (Canon) on Apr 08, 2015 at 19:09 UTC
    And what about this:
    system ("/path/ffmpeg -i $mp3");
    or possibly:
    system ("cat my.mp3 | ffmpeg -i pipe:0");

    Je suis Charlie.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1122829]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found