Richard Knop
Richard Knop

Reputation: 83697

Cannot run ffmpeg in subproces.call

So, I have a simple class where I am trying to save a string response from a terminal ffmpeg command into an object property:

import os
import subprocess

class Movie(object):

    absolute_path = None
    movie_info = None

    def __init__(self, path):
        self.absolute_path = "%s/%s" % (os.getcwd(), path)
        if(os.path.exists(self.absolute_path) is False):
            raise IOError("File does not exist")

    def get_movie_info(self):
        ffmpeg_command = "ffmpeg -i %s" % self.absolute_path
        self.movie_info = subprocess.call(ffmpeg_command)
        print self.movie_info

When I then run this command in cmd:

import os
import sys
sys.path.append(os.getcwd())

from Encode.Movie import Movie

try:
    movie = Movie("tests/test_1.mpg")
    movie.get_movie_info()
except IOError as e:
    print e

I get this exception:

richard@richard-desktop:~/projects/hello-python$ python main.py 
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    movie.get_movie_info()
  File "/home/richard/projects/hello-python/Encode/Movie.py", line 16, in get_movie_info
    self.movie_info = subprocess.call(ffmpeg_command)
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

The path is correct because when I do print self.absolute_path before subprocess.call(), I get:

/home/richard/projects/hello-python/tests/test_1.mpg

And this file exists.

Upvotes: 1

Views: 3289

Answers (3)

Richard Knop
Richard Knop

Reputation: 83697

I actually used this way of getting the output from ffmpeg as it is an error output:

    ffmpeg_command = ["avconv", "-i", self.absolute_path]
    p = Popen(ffmpeg_command, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()

Upvotes: 1

German Rumm
German Rumm

Reputation: 5812

BTW, If you want to store the output of a command in a variable, then you should use check_output instead of a call

http://docs.python.org/library/subprocess.html#subprocess.check_output

Upvotes: 1

glglgl
glglgl

Reputation: 91028

The problem is

ffmpeg_command = "ffmpeg -i %s" % self.absolute_path
self.movie_info = subprocess.call(ffmpeg_command)
  • you give a single string as command line, but you omit the parameter shell=True.

The recommended way is, however, to do

ffmpeg_command = ["ffmpeg", "-i", self.absolute_path]
self.movie_info = subprocess.call(ffmpeg_command)

in order to give the command and arguments separately. This way, you have no problems with quoting etc, and you omit an unnecessary shell call.

Upvotes: 6

Related Questions