3

I'm trying to store the dimensions of video files in a folder into a list.

#!/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

The resulting output of this program is:

384x288640x480480x350352x162480x360640x360480x360384x288240x180320x240480x360384x288346x26

When I run the mediainfo command in the terminal, each video dimension is on a newline:

384x288
640x480
480x350
352x162
480x360

I want each dimension stored as a seperate item in a list. I'm trying to iterate over stdout but it doesn't work.

UPDATE #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

This seems to give me the list, thanks to @Chakib's suggestion.

['440x360', '320x240', '480x360', '320x240', '480x360', '320x240', '320x240', '400x224', '']
koogee
  • 943
  • 3
  • 12
  • 24
  • 2
    you habe to escape `\n` in your Popen call like this `\\n` – sp4ke Apr 03 '13 at 12:27
  • works with \\n but output has an extra newline between items e.g. `340X480` \n `480X240` – koogee Apr 03 '13 at 13:40
  • How would I do it if my command looked like `itemsListEXT = subprocess.check_output(cmd, shell=True,executable="/bin/bash").splitlines() return itemsListEXT` – Jonathan Komar Aug 01 '14 at 22:28
  • answered my own question. it is easy (just change splitlines() to split(): `itemsListEXT = subprocess.check_output(cmd,shell=True,executable="/bin/bash").split() return itemsListEXT` – Jonathan Komar Aug 01 '14 at 23:14

1 Answers1

3

\Is this what you want?

#!/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
Hal Canary
  • 2,154
  • 17
  • 17