输入:
$ ./ffmpeg -i test020.3gp
ffmpeg version UNKNOWN, Copyright (c) 2000-2011 the FFmpeg developers
built on May 5 2011 14:30:25 with gcc 4.4.3
configuration:
libavutil 51. 2. 0 / 51. 2. 0
libavcodec 53. 3. 0 / 53. 3. 0
libavformat 53. 0. 3 / 53. 0. 3
libavdevice 53. 0. 0 / 53. 0. 0
libavfilter 2. 4. 0 / 2. 4. 0
libswscale 0. 14. 0 / 0. 14. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test020.3gp':
Metadata:
major_brand : 3gp4
minor_version : 512
compatible_brands: 3gp4
creation_time : 2004-07-01 09:59:21
Duration: 00:01:02.20, start: 0.000000, bitrate: 284 kb/s
Stream #0.0(und): Audio: aac, 44100 Hz, stereo, s16, 96 kb/s
Metadata:
creation_time : 2004-07-01 09:59:21
Stream #0.1(und): Video: mpeg4, yuv420p, 176x120 [PAR 1:1 DAR 22:15], 184 kb/s, 15 fps, 15 tbr, 30k tbn, 15 tbc
Metadata:
creation_time : 2004-07-01 09:59:23
At least one output file must be specified
假设我想提取宽度和宽度。高度使用以下正则表达式:
(\d+x\d+)
使用perl,我会做这样的事情:
$ ./ffmpeg -i test020.3gp 2>&1 | perl -lane 'print $1 if /(\d+x\d+)/'
176x120
然后我尝试构建一个类似的python单行程序,它有点工作,但并不完美:
$ ./ffmpeg -i test020.3gp 2>&1 | python -c "import sys,re;[sys.stdout.write(str(re.findall(r'(\d+x\d+)', line))) for line in sys.stdin]"
[][][][][][][][][][][][][][][][][][][]['176x120'][][][]
对于perl one,python单行看起来是什么样的?
答案 0 :(得分:5)
您想要的是re.search
而不是re.findall
。
这就是诀窍,即使单行本身有点“难看”(/tmp/p
只是你给出的样本数据):
% cat /tmp/p 2>&1 | python -c "import re,sys; print re.search(r'(\d+x\d+)', sys.stdin.read()).group()"
176x120
您是不是只使用grep
(在这种情况下为egrep
)?
% cat /tmp/p | egrep -o '[0-9]+x[0-9]+'
176x120
答案 1 :(得分:2)
cat sample.txt | python -c "import sys,re; print '\n'.join(re.findall(r'(\d+x\d+)',sys.stdin.read()))"
176x120
答案 2 :(得分:2)
我有一个module in the works试图让编写Python单行的任务变得更加愉快。人们可以将其视为Ruby和Perl的Python -n
,-e
,-l
,-p
选项。
$ pip install oneliner
# use as pyl-$major-$minor <args> or python -m oneliner <args>
$ pyl-2.7 -j -ne 're.findall("\d+x\d+", line)' < ffmpeg.txt