Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with running an FFMPEG command from within a Python script. When I run the following command from the terminal, I can stream video and audio from my attached webcam (Logitech C310) and output to file "out.avi" without any errors.

ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi

When I run the same command in a Python script below,

def call_command(command):
    subprocess.Popen(command.split(' '))

call_command("ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi")

it gives me this error:

Input #0, alsa, from 'default':
  Duration: N/A, start: 1317762562.695397, bitrate: N/A
  Stream #0.0: Audio: pcm_s16le, 44100 Hz, 1 channels, s16, 705 kb/s
[video4linux2 @ 0x165eb10]Cannot find a proper format for codec_id 0, pix_fmt -1.
/dev/video0: Input/output error

Could anyone shed some light on what could be going on here? I've tried using os.system() as well as and it gives me the same errors. I'm not sure where to start on what could be going wrong here. I tried searching for the "video4linux2 Cannot find a proper format for codec_id 0, pix_fmt -1" error, but couldn't find anything consistent.

share|improve this question

closed as too localized by James Mar 6 '12 at 15:54

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

I would place the actual command string in a variable and then print the variable to stdout to ensure that you do not need to escape any special characters. Might give you clue as to what is going on. – Justin Andrusk Oct 5 '11 at 0:53
Well, what I've also tried is putting the command in a shell script "", and giving it executable permissions. I then open terminal, and run "./", and it works. When I try calling the shell command "./" from within my Python script, I'm left with the original error as before. This is the Python command I tried with the script:["./"]) – 12hys Oct 5 '11 at 1:13
Are you still experiencing this issue? – James Feb 27 '12 at 23:42
No, this was moved to Stack Overflow, where I solved the issue: – 12hys Feb 28 '12 at 19:54
up vote 0 down vote accepted

This issue has been solved in stackoverflow.

To summarize the answer, it was a problem with the asker code, he was displaying the output on the screen at the same time that he was trying to record it and this caused a conflict.

share|improve this answer
This answer is trying to close an old dead post. Please accept it if you are the OP or vote it up to close this question. Note that it is a community wiki, no rep gain or loss, everyone can edit. – Javier Rivera Mar 1 '12 at 10:54

Rather than splitting your "command" string you can call the subprocess module with parameter shell=True:

subprocess.Popen(command, shell=True)

However we can't test here if this resolves your codec issue.

share|improve this answer
Nope, this doesn't work. I've already tried. The program exits and ffmpeg spits out it's version number, libraries, and this: 'Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... Use -h to get full help or, even better, run 'man ffmpeg'' – 12hys Oct 5 '11 at 16:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.