正则表达式从字符串导出一些信息

时间:2013-11-07 05:17:38

标签: php regex

我有一个像这样的输出字符串

ffmpeg version 0.8.8-4:0.8.8-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
  built on Oct 22 2013 12:31:55 with gcc 4.6.3
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
[flv @ 0x23307a0] Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000/1) -> 25.00 (25/1)
Input #0, flv, from '307263_191.flv':
  Metadata:
    metadatacreator : Yet Another Metadata Injector for FLV - Version 1.4
    hasKeyframes    : true
    hasVideo        : true
    hasAudio        : true
    hasMetadata     : true
    canSeekToEnd    : true
    datasize        : 42773717
    videosize       : 40350092
    audiosize       : 2403669
    lasttimestamp   : 107
    lastkeyframetimestamp: 107
    lastkeyframelocation: 42758866
  Duration: 00:01:47.18, start: 0.000000, bitrate: 3186 kb/s
    Stream #0.0: Video: flv, yuv420p, 1024x576, 3009 kb/s, 25 tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: adpcm_swf, 22050 Hz, 2 channels, s16, 176 kb/s
At least one output file must be specified

我必须从字符串中获取一些信息,如持续时间,视频比特率音频比特率和视频和音频的大小。 一种方法是爆炸,但字符串的重要部分之前和之后的文本不在我的控制中,并可能在每个输入上改变

是否可以通过:

匹配字符串中的任何键和值

更新: 我需要这些

videosize       : 40350092
audiosize       : 2403669
Duration: 00:01:47.18

    Stream #0.0: Video: flv, yuv420p, 1024x576, 3009 kb/s, 25 tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: adpcm_swf, 22050 Hz, 2 channels, s16, 176 kb/s

3 个答案:

答案 0 :(得分:1)

请尝试以下操作。

preg_match_all('/(?:\b(?:duration|videosize|audiosize)\b\s*:[0-9:. ]+|stream #.*)/i', $data, $matches);
print_r($matches);

请参阅working demo

输出

Array
(
    [0] => Array
        (
            [0] => videosize       : 40350092
            [1] => audiosize       : 2403669
            [2] => Duration: 00:01:47.18
            [3] => Stream #0.0: Video: flv, yuv420p, 1024x576, 3009 kb/s, 25 tbr, 1k tbn, 1k tbc
            [4] => Stream #0.1: Audio: adpcm_swf, 22050 Hz, 2 channels, s16, 176 kb/s
        )
)

答案 1 :(得分:0)

尝试(未经测试):

$duration     = preg_replace("#duration\s*:\s*(\d{2}:\d{2}:\d{2}.\d+)#i", "$1", $input)
$datasize     = preg_replace("#datasize\s*:\s*(\d+)#i"                  , "$1", $input)
$audiobitrate = preg_replace("#audio\s*:.*,\s*(\d+)\s*kb/s#i"           , "$1", $input)

我使用“datasize”做的同样的事情应该适用于你想要获得的每一个信息,尽管它们具有不同的格式(如持续时间和内部音频比特率的流信息)

答案 2 :(得分:0)

preg_match_all('/ {2,4}(.+?)\s?: (.+)/',$ffmegoutputstring,$matches,PREG_SET_ORDER);
print_r($matches);

...输出

Array
(
    [0] => Array
        (
            [0] =>     metadatacreator : Yet Another Metadata Injector for FLV - Version 1.4
            [1] => metadatacreator
            [2] => Yet Another Metadata Injector for FLV - Version 1.4
        )

    [1] => Array
        (
            [0] =>     hasKeyframes    : true
            [1] => hasKeyframes
            [2] => true
        )