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', '']
4
  • 2
    you habe to escape \n in your Popen call like this \\n Commented Apr 3, 2013 at 12:27
  • works with \\n but output has an extra newline between items e.g. 340X480 \n 480X240 Commented Apr 3, 2013 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 Commented Aug 1, 2014 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 Commented Aug 1, 2014 at 23:14

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Returns: ['480x360480x350320x240384x288640x4801280x720450x360480x3601280x720']. I want ['480x360', '480x350'...]
sounds like a problem in the cmdline arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.