Вопрос

I used FFmpeg to record video on my Raspberry PI. The code is here:

ffmpeg -f video4linux2 -y -r 4 -i /dev/video0 -vf "drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf:expansion=strftime:text='%Y-%m-%d %H\\:%M\\:%S': fontcolor=white:box=1:boxcolor=black@0.8:x=w-text_w:y=h-line_h" -vframes 20 -vcodec mpeg4 out.mp4 

I ran this code in terminal and it worked fine. However I need to run this by using Python. Then I wrote this code which shown below:

from subprocess import Popen
from os import system
x = "drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf:expansion=strftime:text='%Y-%m-%d %H\\:%M\\:%S': fontcolor=white:box=1:boxcolor=black@0.8:x=w-text_w:y=h-line_h"
result = ['ffmpeg', '-f', 'video4linux2', '-y', '-r', '4', '-i', '/dev/video0', '-vf', x, '-vframes ','20', '-vcodec', 'mpeg4', 'out.mp4']
Popen(result)

It is working for only a very short time (absolutely less than 1s). Anything wrong with it?

Это было полезно?

Решение

I think I figured it out. It looks like you are having an issue with bash white space. On the command line, you enclosed the entire -vf option in quotes. In the python script, you created x as a string; Popen would interpret the string like the other arguments you have in the list and end up leaving it unquoted in the actual command run. On the command line, this would look like:

ffmpeg -f video4linux2 -y -r 4 -i /dev/video0 -vf drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf:expansion=strftime:text='%Y-%m-%d %H\\:%M\\:%S': fontcolor=white:box=1:boxcolor=black@0.8:x=w-text_w:y=h-line_h -vframes 20 -vcodec mpeg4 out.mp4

So in effect, you would want:

x = '"drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf:expansion=strftime:text=\'%Y-%m-%d %H\\:%M\\:%S\': fontcolor=white:box=1:boxcolor=black@0.8:x=w-text_w:y=h-line_h"'

Otherwise, that argument would be split at the white space in the date format when actually run by bash and cause some unexpected behavior.

(Sorry for the wall of text, just want to make sure that I'm not confusing.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top