duanmeng1862
duanmeng1862
2011-10-26 15:20

系统功能php问题

已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douye1876 douye1876 10年前

    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;
       }
    }
    
    点赞 评论 复制链接分享
  • doudou5421 doudou5421 10年前

    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.

    点赞 评论 复制链接分享