0

I am trying to use system function. The command that I execute return multiple lines of output. Sample output:

C:\wamp\www\WordPress\wp-content\plugins\audio-player\app\uploads>ffmpeg -i 4ea8
23b13639d.mp3
ffmpeg version N-34031-ge403a97, Copyright (c) 2000-2011 the FFmpeg developers
  built on Oct 25 2011 15:06:47 with gcc 4.6.1
  configuration: --enable-gpl --enable-version3 --enable-runtime-cpudetect --ena
ble-avisynth --enable-bzlib --enable-frei0r --enable-libopencore-amrnb --enable-
libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --ena
ble-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --en
able-libtheora --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis
--enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib

  libavutil    51. 22. 0 / 51. 22. 0
  libavcodec   53. 23. 0 / 53. 23. 0
  libavformat  53. 17. 0 / 53. 17. 0
  libavdevice  53.  4. 0 / 53.  4. 0
  libavfilter   2. 45. 0 /  2. 45. 0
  libswscale    2.  1. 0 /  2.  1. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[mp3 @ 01C98580] max_analyze_duration 5000000 reached at 5015510
[mp3 @ 01C98580] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '4ea823b13639d.mp3':
  Metadata:
    title           : Gori
    track           : 2
    artist          : A Band Of Boys
    genre           : genre
    TLEN            : 253753
  Duration: 00:04:13.88, start: 0.000000, bitrate: 128 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16, 128 kb/s
At least one output file must be specified

I need to parse duration out of this output. But for that the 1st step is to actually get the output in a string. However system command only returns last line as per documentation. How can I get around this issue?

Jaggu
  • 6,298
  • 16
  • 58
  • 96
  • I think you want to use `passthru()` or `exec()` http://stackoverflow.com/questions/732832/php-exec-vs-system-vs-passthru – JackWink Oct 26 '11 at 15:23

2 Answers2

1

Use exec() instead.

<?php

exec("ffmpeg -i 4ea823b13639d.mp3", $output);

?>

$output would be an array of every line of output from the command given.

Alternatively, use shell_exec() or the backtick operator. Those will return the entire output of the program into one big string instead of an array of strings.

WWW
  • 9,734
  • 1
  • 29
  • 33
1

Instead of system(), call exec() which places all lines of output into an array of lines.

Then loop over the array to find the line containing the duration. exec("ffmpeg -options...", $output);

// $output now holds an array of lines
foreach ($output as $line) {

   // Trim whitespace and look for Duration in the first position
   if (strpos(trim($line), "Duration") === 0) {
     echo $line;
     // Cut off the bitrate info
     list($durpart, $bitpart) = explode(",", $line);
     echo $durpart;
   }
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390