3

我正在尝试将文件夹中视频文件的尺寸存储到列表中。

#!/usr/bin/env python

# src_dimensions.py

# requires mediainfo package

import subprocess

proc = subprocess.Popen('mediainfo --Inform="Video;%Width%x%Height%\n" /home/saad/Videos/*.*', shell=True, stdout=subprocess.PIPE)

for line in iter(proc.stdout.readline,''):
   print line

该程序的结果输出是:

384x288640x480480x350352x162480x360640x360480x360384x288240x180320x240480x360384x288346x26

当我mediainfo在终端中运行命令时,每个视频维度都在换行符上:

384x288
640x480
480x350
352x162
480x360

我希望每个维度都作为单独的项目存储在列表中。我正在尝试迭代,stdout但它不起作用。

更新#1

import subprocess
dim = []
proc = subprocess.Popen('mediainfo --Inform="Video;%Width%x%Height%\\n" /home/saad/Videos/*.*', shell=True, stdout=subprocess.PIPE)
for line in iter(proc.stdout.readline, ''):
   dim.append(line.rstrip('\n'))
print dim

感谢@Chakib 的建议,这似乎给了我列表。

['440x360', '320x240', '480x360', '320x240', '480x360', '320x240', '320x240', '400x224', '']
4

1 回答 1

3

\这是你想要的吗?

#!/usr/bin/env python
# src_dimensions.py
# requires mediainfo package
import subprocess, glob
globpattern = 'path/to/*.*'
cmd = ['mediainfo', '--Inform=Video;%Width%x%Height%\\n']
cmd.extend(glob.glob(globpattern))
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE)
outputlines = filter(lambda x:len(x)>0,(line.strip() for line in proc.stdout))
print outputlines
于 2013-04-03T13:38:32.660 回答