Question

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 the 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 subprocess.call() 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.

I've also tried putting the "ffmpeg -f..." command in a shell script "test.sh", and giving it executable permissions. I then open terminal, and run "./test.sh", and it works. When I try calling the command "./test.sh" from within my Python script, I'm left with the original error as before. This is the Python command I tried with the test.sh script:

subprocess.call(["./test.sh"])
Était-ce utile?

La solution

I have fixed the issue. In my Python script, I'm using OpenCV to display these frames as well as record them using ffmpeg. There is a conflict when trying to run the ffmpeg command and display them on the screen using OpenCV.

More specifically, when creating a OpenCV CreateCameraCapture object:

from opencv.cv import *  
from opencv.highgui import *

capture = cvCreateCameraCapture(0) #conflict with ffmpeg/v4l2 occurs here

Commenting out that line of code fixes my problem. There aren't any issues with Python and executing commands.

Autres conseils

You should try running Popen with shell=True argument.

subproc = subprocess.popen(command.split(' '), shell=True)

Shell =True is not a good option in most cases. It do not works if you need pipe as output. Use

pipe_stdin=True

It solves the problem for me.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top