当前位置: 动力学知识库 > 问答 > 编程问答 >

system function php issue

问题描述:

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?

网友答案:

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;
   }
}
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: