[GRLUG] a little regex help

Roger Roelofs roger.roelofs at gmail.com
Sun Jun 13 14:09:16 UTC 2010


Topher,

On 06/12/2010 11:41 PM, topher at t1kdevelopment.com wrote:
> I suck at regex, and I'm tired, so I turn to you guys.
>
> Using php, I'm going to exec ffmpeg to spit out the details of a 
> video, like this:
>
> ffmpeg -i FLIP_2_091128-3.mp4
>
> I want to know the duration of the video.  The output of that command 
> is thus:
>
> FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, 
> et al.
>   configuration: --prefix=/usr --libdir=/usr/lib64 
> --mandir=/usr/share/man --incdir=/usr/include/ffmpeg 
> --extra-cflags=-fPIC --enable-libmp3lame --enable-libogg 
> --enable-libvorbis --enable-libogg --enable-libtheora --enable-libfaad 
> --enable-libfaac --enable-libgsm --enable-xvid --enable-x264 
> --enable-liba52 --enable-liba52bin --enable-pp --enable-shared 
> --enable-pthreads --enable-gpl --disable-strip
>   libavutil version: 49.4.0
>   libavcodec version: 51.40.4
>   libavformat version: 51.12.1
>   built on Jun  4 2007 10:46:34, gcc: 4.1.1 20070105 (Red Hat 4.1.1-52)
> Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'FLIP_2_091128-3.mp4':
>   Duration: 00:01:21.5, start: 0.000000, bitrate: 812 kb/s
>   Stream #0.0(und): Video: h264, yuv420p, 720x480, 29.97 fps(r)
>   Stream #0.1(und): Audio: aac, 44100 Hz, stereo
> Must supply at least one output file
>
>
>
> You can see the Duration printed there quite nicely.  Which of the php 
> preg functions would I want to use, and what would I ask of it?
>
> I'm not terribly concerned about the speed of the method.  I also 
> really only want the HH:MM:SS part, not even the fractional seconds.

Assuming $data contains the output above,

$data = str_replace("\n", "", $data);    // put all on one line to make 
regex easier
preg_match_all("/Duration: (.+)\./U", $data, $matches);     // using 
ungreedy flag to stop at first (.)
echo $matches[1][0];

Roger

(You're welcome)


More information about the grlug mailing list