Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

ffmpeg console progress bar

by strredwolf (Chaplain)
on Feb 18, 2013 at 17:01 UTC ( [id://1019371]=CUFP: print w/replies, xml ) Need Help??

If you're in the furry fandom, more often than not you heard of the videos of a certain Big Blue Fox by the name of BBF TV HD. They're by a German video editor who works at RTL, and of when he and the Eurofurance crew visits a furry convention. It's a great way to get an idea of what a con is, or if you missed it, what happened.

I pulled most of the videos from his site, and every so often I tweak the encode settings to mencoder to do it... except mencoder doesn't do bframes right. I switched over to ffmpeg (not avconvert from the rogue libav gang; the original ffmpeg)... which doesn't have a nice status line.

Thankfully ffmpeg hit 1.0 and got a -progress option, which gives a nice status readout. Too bad my build outputs to a file, md5 code, or a pipe.. aka an opened file descriptor. Forget named pipes, it'll recreate 'em as a file.

After some hacking around... I have this! The settings in the ffmpeg command match that for a 2nd Gen AppleTV, but I also have info for the 3rd Gen/iPhone 5/iPad 4. I name this version atv-enc.pl. It takes two arguments: atv-enc.pl input output

EDIT: Whoops, a few bugs crept in with the bar. Fixed, and enhanced a bit.

#!/usr/bin/perl use POSIX qw(mkfifo); use Fcntl; use IO::Handle; use strict; $|=1; print "############\n"; my %prog; my $infile=$ARGV[0]; my $outfile=$ARGV[1]; # get some stats my $frametotal,$fwide,$fhigh; open(IN,"ffprobe -show_streams '$infile' 2>/dev/null |") || die "$infile: $!"; while(<IN>) { chomp; $frametotal=$1 if(/^nb_frames=(\d+)/); $fwide=$1 if(/^width=(\d+)/); $fhigh=$1 if(/^height=(\d+)/); last if(/^\[\/STREAM\]/); } close(IN); my @acmd; @acmd=("-acodec","libfaac","-ac","2","-ab","160k"); my @vcmd=qw/-c:v libx264 -vprofile main -preset slow -tune film -level + 3.1 -crf 28 -threads 0/; if($fwide > 1280 || $fhigh > 720) { @vcmd=(@vcmd,"-vf","scale=min(1280\\,iw):-1"); } pipe(IN, OUT); # We read from IN, ffmpeg writes to OUT. OUT->autoflush(1); # Autoflush on, close-on-exec off my $val=fcntl(OUT,F_GETFD,0) || die "Error: fcntl GET: $!"; $val &= ~FD_CLOEXEC; fcntl(OUT,F_SETFD,$val) || die "Error: fcntl SET: $!"; my $fd=fileno OUT; my @cmd=("ffmpeg","-progress","pipe:$fd","-v","panic", "-i",$infile, @acmd,@vcmd,@metadata,$outfile); my $pid=fork(); # Fork the kid unless($pid) { die "Can't fork! $!" unless defined $pid; close(IN); exec @cmd or die "Can't exec! $!"; } # parent. close(OUT); while(<IN>) { chomp; my ($a,$b)=split /=/; $prog{$a}=$b; if($a eq "progress") { my $pcent=$prog{"frame"}/$frametotal*100; my $hcent=int($pcent/2); my $bar="[".("#" x $hcent).(" " x (50-$hcent))." ] "; $bar .=sprintf("%.2f",$pcent)."% fps=".$prog{"fps"}; print "\r$bar"; } } print "\nWaiting for finish...\n"; waitpid($pid,0);

Information doesn't want to be free. It wants to be feline.

Replies are listed 'Best First'.
Re: ffmpeg console progress bar
by jwkrahn (Abbot) on Feb 20, 2013 at 02:28 UTC
    my $frametotal,$fwide,$fhigh;

    You need to enclose the list of variables in parentheses or my will only work on $frametotal.

    my ( $frametotal, $fwide, $fhigh );


    @vcmd=(@vcmd,"-vf","scale=min(1280\\,iw):-1");

    That is usually written as:

    push @vcmd, "-vf", "scale=min(1280\\,iw):-1";


    if($a eq "progress") { my $pcent=$prog{"frame"}/$frametotal*100; my $hcent=int($pcent/2); my $bar="[".("#" x $hcent).(" " x (50-$hcent))." ] "; $bar .=sprintf("%.2f",$pcent)."% fps=".$prog{"fps"}; print "\r$bar"; }

    I would probably write that as:

    next if $x ne 'progress'; my $pcent = $prog{ frame } / $frametotal * 100; printf '%s[%-50s ] %.2f%% fps=%s', "\r", '#' x int( $pcent / 2 ), +$pcent, $prog{ fps };

Log In?
Username:
Password:

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

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

    No recent polls found