0

我已经开始使用ffmpeg了,我对它很陌生,所以请多多包涵。

我已经安装ffmpeg在我的服务器上,效果很好;通过 ssh 登录时,我可以运行某些命令并获取输出数据

例如我可以运行

ffmpeg -i Sleep\ Away.mp3 

它返回以下内容:

ffmpeg version 0.8.5, Copyright (c) 2000-2011 the FFmpeg developers
  built on Aug 20 2012 09:28:43 with clang 3.1 (tags/Apple/clang-318.0.61)
  configuration: --enable-nonfree --enable-gpl --enable-version3 --enable-postproc --enable-swscale --enable-avfilter --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libfaac --enable-libxvid --enable-libx264 --enable-libvpx --enable-hardcoded-tables --enable-shared --enable-pthreads --disable-indevs --cc=clang
  libavutil    51.  9. 1 / 51.  9. 1
  libavcodec   53.  7. 0 / 53.  7. 0
  libavformat  53.  4. 0 / 53.  4. 0
  libavdevice  53.  1. 1 / 53.  1. 1
  libavfilter   2. 23. 0 /  2. 23. 0
  libswscale    2.  0. 0 /  2.  0. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[mp3 @ 0x7f9694011a00] Header missing
    Last message repeated 13 times
[mp3 @ 0x7f9694007c00] max_analyze_duration 5000000 reached at 5007020
[mp3 @ 0x7f9694007c00] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'Sleep Away.mp3':
  Metadata:
    track           : 3
    album           : Bob Acri
    artist          : Bob Acri
    title           : Sleep Away
    genre           : Jazz
    album_artist    : Bob Acri
    composer        : Robert R. Acri
    date            : 2004
  Duration: 00:03:21.77, start: 0.000000, bitrate: 192 kb/s
    Stream #0.0: Audio: mp3, 44100 Hz, stereo, s16, 192 kb/s
At least one output file must be specified

我要问的问题是,如何使用上面的输出数据?我正在开发一个音乐网站;假设我想遍历所有 MP3 文件并将有关它们的信息保存到数据库中,以便上述结果:

Sleep Away.mp3    mp3    3:21    Jazz    2004     Bob Acri ...

显然在一张桌子上

到目前为止,我尝试使用 php反引号运算符但没有成功。我只是想我会在这里提出一个问题,以从做过类似事情的人那里获得一些建议。

谢谢

更新:我尝试了以下

<?php $output = `ffmpeg -i Sleep\ Away.mp3`; echo "<pre>$output</pre>"; ?>
<?php $output = shell_exec('ffmpeg -i Sleep\ Away.mp3'); echo "<pre>$output</pre>"; ?>

两者似乎都没有返回任何东西。

4

3 回答 3

4

ffmpeg发送有关标准错误的信息,而不是捕获的标准输出。shell_exec()

您需要2>&1在命令末尾添加:

$out = shell_exec('/path/to/ffmpeg -i Sleep\ Away.mp3 2>&1');
于 2012-08-22T13:51:02.383 回答
1

So you want to execute a shell command (ffmpeg in your case) from php, and capture its output, right?

Backtick Operator or the shell_exec function are a good start for that.

Note that these only work when safe mode is disabled and when shell_exec function is not disabled (via disable_functions setting). If your scripts are running on a shared hoster, you might not have access to these configurations, but you should at least be able to check them via phpinfo().

How you specify the path to any file you are referencing in the command is also crucial - your php script might not run with the working folder you expect it to run; so it might be best to reference to all files via absolute path.

于 2012-08-22T13:32:50.307 回答
1

改用FFprobe

ffprobe -loglevel error -show_format -show_streams input.mp3

FFmpeg 用于处理视频,FFprobe 是获取视频信息的配套工具。它以各种易于解析的格式输出数据,而不需要解析 FFmpeg 在导入视频时偶然吐出的诊断消息。

于 2012-08-22T22:18:35.860 回答