Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Print Problem: Progress Printout Prevented

by pobocks (Chaplain)
on Jan 06, 2009 at 08:18 UTC ( [id://734367]=perlquestion: print w/replies, xml ) Need Help??

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

Update: The problem was that, even with autoflush set, the <> operator expects a newline before it returns. Thanks to Ikegami and Bart, for slapping the solution into my head, and to Ikegami in particular for his much saner 'open'.

Also, I feel it only just to point out that they both had suggestions that essentially turn this into a single-line program; the simplest solution being to simply exec('mencoder', $arg1, ..., '-o', $arg2);

I fall, another victim of overengineering through ignorance/inexperience/4AM thinking.

I'm writing a small convenience script that is essentially a wrapper for single run of mencoder.

I have something that works, in that it runs mencoder with the proper options and arguments. My problem is passing what mencoder is printing back out through Perl to the console.

Note, most of the output is passed through fine. It's only that the constantly updating progress line at the bottom doesn't get printed, ever.

Here's the code. It's meant to convert a video file into the proper format to run on my new Creative Zen (Xmas present) if anyone cares ;-)

#!/usr/bin/perl -w # zenconv.pl --- Converts Files for play on Creative Zen # Author: William Mayo <pobocks@davemayo.is-a-geek.org> # Created: 06 Jan 2009 # Version: 0.01 use warnings; use strict; die "Wrong Number of Args, stupid!\n" if (@ARGV != 2); my $arg1 = shift; my $arg2 = shift; $arg1 =~ s/ /\\ /g; $arg2 =~ s/ /\\ /g; open (CONVERSION, '-|', "mencoder $arg1 -oac mp3lame -aid 128 -ovc xvi +d -xvidencopts bitrate=-1 -vf scale -zoom -xy 320 -o $arg2"); { local $| = 1; while ( <CONVERSION>) { print $_; } } close (CONVERSION);
for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

Replies are listed 'Best First'.
Re: Print Problem: Progress Printout Prevented
by ikegami (Patriarch) on Jan 06, 2009 at 09:29 UTC

    For those without the benefit of being in the CB at the time, this problem has been resolved.

    It's only that the constantly updating progress line at the bottom doesn't get printed, ever.

    Turns out that wasn't true. All the updates were being printed at once at the end. <> blocks until a newline is received, but mencoder wasn't sending any between the updates. Switching to getc solved the problem. Switching to sysread should also work, but more efficiently.

    #!/usr/bin/perl -w # zenconv.pl --- Converts Files for play on Creative Zen use warnings; use strict; die "Wrong Number of Args, stupid!\n" if (@ARGV != 2); open(my $conversion, '-|', mencoder => $ARGV[0], -oac => 'mp3lame', -aid => 128, -ovc => 'xvid', -xvidencopts => 'bitrate=-1', -vf => 'scale', -zoom, -xy => 320, -o => $ARGV[1], ); { local $| = 1; while ( sysread($conversion, my $buf, 4096) ) { print $buf; } }
Re: Print Problem: Progress Printout Prevented
by ikegami (Patriarch) on Jan 06, 2009 at 08:46 UTC

    Sounds like mencoder's behaviouris dependent on whether it's connected to a terminal or not. If so, you want to look a pseudo ttys. I'd look at IO::Pty, possibly called via IO::Run.

    By the way,

    $arg1 =~ s/ /\\ /g; $arg2 =~ s/ /\\ /g;

    is very badly done. Use the multiple argument form to avoid having to quote at all.

    open(CONVERSION, '-|', mencoder => $arg1, -oac => 'mp3lame', -aid => 128, -ovc => 'xvid', -xvidencopts => 'bitrate=-1', -vf => 'scale', -zoom, -xy => 320, -o => $arg2, );
Re: Print Problem: Progress Printout Prevented
by ikegami (Patriarch) on Jan 06, 2009 at 09:41 UTC
    If the goal is simply to have a wrapper that supplies the arguments, all you need is
    #!/usr/bin/perl -w # zenconv.pl --- Converts Files for play on Creative Zen use warnings; use strict; die "Wrong Number of Args, stupid!\n" if (@ARGV != 2); exec( mencoder => $ARGV[0], -oac => 'mp3lame', -aid => 128, -ovc => 'xvid', -xvidencopts => 'bitrate=-1', -vf => 'scale', -zoom, -xy => 320, -o => $ARGV[1], );

    If you need to do some post-processing, replace exec with system.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found