读取mplayer/ffmpeg运行时的输出信息

  • user warning: Table './zgq_smzz/blog_comments' is marked as crashed and should be repaired query: SELECT COUNT(*) FROM blog_comments c WHERE c.nid = 21 AND c.status = 0 in /home/www/zhouguoqiang/zhoume.org/modules/comment/comment.module on line 992.
  • user warning: Table './zgq_smzz/blog_comments' is marked as crashed and should be repaired query: SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data, c.thread, c.status FROM blog_comments c INNER JOIN blog_users u ON c.uid = u.uid WHERE c.nid = 21 AND c.status = 0 ORDER BY c.thread DESC LIMIT 0, 50 in /home/www/zhouguoqiang/zhoume.org/modules/comment/comment.module on line 992.

由于使用了退格(backspace)、覆盖(substitute)、垂直方向(vertical tab)的tab等控制字符,很难直接读到mplayer/ffmpeg运行时的信息。其实按字符读取并过滤其中的控制字符,便可以还原其输出信息的“真面目“了。以下是在php中的实现:

< ?php

$src = '/path/to/input.mp3';
$dst = '/path/to/output.flv';

$cmd = '/usr/local/bin/ffmpeg -i "%s" -acodec libmp3lame  -f flv   -y "%s" 2>&1 ';
$cmd = sprintf($cmd, $src, $dst);
$handle = popen($cmd, 'r');
while(!feof($handle)) {
    // 按字符读取
    $charset = fgetc($fd);

    // 如果是垂直tab符号
    if ($ord($charset) == 13) {
        printf("%s\n", $line);
        $line = '';
        continue;
    }

    // 如果是退格或覆盖符号
    if (ord($charset) == 10 || ord($charset) == 32) {
        continue;
    }
    $line .= $charset;
}
pclose($handle);

?>

ps: 看不出wordpress有时也很bt, 昨天这篇文章发了半天都返回503错误,后来发现是文章里popen一词导致的,我只好用字符p的html实体&#112;来代替了

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options