正则表达式:从FFMPEG日志获取FPS

时间:2011-02-24 09:51:55

标签: php regex ffmpeg preg-match

我尝试再次使用正则表达式,我只是不得到它。我正在尝试遍历FFMPEG的日志文件并获取FPS。

基本上,在日志文件的变量行(似乎大约是16/17)上出现这一行: -

Stream #0.1[0x1e0]: Video: mpeg1video, yuv420p, 640x480 [PAR 1:1 DAR 4:3], 104857 kb/s, 25 fps, 25 tbr, 90k tbn, 25 tbc

我想逐行遍历,我认为我可以通过/ n进行爆炸然后进行循环,但我更喜欢只获得该行的行,然后获取FPS值。 / p>

感激地收到任何指示。

1 个答案:

答案 0 :(得分:4)

你可以试试这个:

if (preg_match('/^Stream #0.*?(\b\d+(?:\.\d+)?\s*fps\b).*/m', $subject, $regs)) {
    $full_line = $regs[0];
    $result = $regs[1];
} else {
    // no match...
}

<强>解释

^     # start of line (/m modifier makes sure that this works)
Stream #0  # match Stream #0 literally
.*?   # match any number of characters, as few as possible
(     # then capture the following
 \b   # starting at a "word" boundary
 \d+  # one or more digits
 (?:  # try to match the following:
  \.  # a dot
  \d+ # followed by one or more digits
 )?   # but make that optional
 \s*  # optional whitespace
 fps  # literal fps
 \b   # end at a word boundary
)     # end of capturing group
.*    # match the rest of the line