NickB
NickB

Reputation: 409

Subprocess call invalid argument or option not found

I'm trying to call ffmpeg command using subprocess.call() on linux, but I'm unable to get the arguments right. Before hand, I used os.system and it worked, but this method is not recommended.

Using arguments with a dash such as "-i" gets me this error

Unrecognized option 'i "rtsp://192.168.0.253:554/user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream"'.
Error splitting the argument list: Option not found

Using arguments without dash like "i" gets me this error

[NULL @ 0x7680a8b0] Unable to find a suitable output format for 'i rtsp://192.168.0.253:554/user=admin&password=&channel=0&stream=0.sdp?real_stream'
i rtsp://192.168.0.253:554/user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream: Invalid argument

Here's the code

class IPCamera(Camera):
"""
    IP Camera implementation
"""
def __init__(self,
             path='\"rtsp://192.168.0.253:554/'
                  'user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream\"'):

    """
        Constructor
    """
    self.path = path

def __ffmpeg(self, nb_frames=1, filename='capture%003.jpg'):
    """
    """

    ffm_input = "-i " + self.path
    ffm_rate = "-r 5"
    ffm_nb_frames = "-vframes " + str(nb_frames)
    ffm_filename = filename

    if platform.system() == 'Linux':
        ffm_path = 'ffmpeg'
        ffm_format = '-f v4l2'

    else:
        ffm_path = 'C:/Program Files/iSpy/ffmpeg.exe'
        ffm_format = '-f image2'

    command = [ffm_path, ffm_input, ffm_rate, ffm_format, ffm_nb_frames, ffm_filename]
    subprocess.call(command)

    print(command)

BTW, I'm running this command on a MT7688.

Thanks

Upvotes: 1

Views: 1326

Answers (1)

Giacomo Alzetta
Giacomo Alzetta

Reputation: 2479

You have to split the options:

command = [ffm_path, '-i', ffm_input, '-r', ffm_rate, '-f', ffm_format, '-vframes',  ffm_nb_frames, ffm_filename]

The ffm_input, ffm_rate, ffm_format should only contain the value:

ffm_input = self.path
ffm_rate = '5'
ffm_nd_frames = str(nb_frames)
ffm_format = 'v412' if platform.system() == 'Linux' else 'image2'

When you pass a list no parsing is done so -r 5 is taken as a single argument but the program expects you to provide two separate arguments -r followed by 5.


Basically if you put them as a single element in the list it's as if you quoted them on the command line:

$ echo "-n hello"
-n hello
$ echo -n hello
hello$

In the first example echo sees a single argument -n hello. Since it does not match any option it just prints it. In the second case echo sees two arguments -n and hello, the first is the valid option to suppress end of line and as you can see the prompt is printed right after hello and not on its own line.

Upvotes: 5

Related Questions