Ffmpeg via subprocess api

Skip to first unread message


Nov 11, 2020, 4:42:23 AM11/11/20
to Tornado Web Server

I am using the following code to run  ffmpeg in tornado:

cmd = ["/usr/bin/ffmpeg", "-threads", "2", "-f", "lavfi", "-i", "anullsrc=r16000:cl=stereo", "-re", "-i", "/dev/video0", "-c:a", "aac", "-strict", "experimental", "-b:a", "128k", "-ar", "44100", "-s", "640x480", "-vcodec", "libx264", "-x264-params", "keyint=120:scenecut=0", "-vb", "200k", "-pix_fmt", "yuv420p", "-f", "flv", "rtmp://a.rtmp.youtube.com/live2/<key>"]
self.__streaming_process = Subprocess(cmd, stdout=Subprocess.STREAM)
IOLoop.instance().add_timeout(time.time() + 10, self.do_fulfill_stop_streaming)

while True:
line = await self.__streaming_process.stdout.read_until(b"\n")
if not line:
self.logger.debug("nothing to show")
await asyncio.sleep(.2)


When i use a similar code for a different Linux process i get new values for the line inside the while loop but for FFmpeg, it just gits once and then does not go to the next line. But in the IDE i see the FFmpeg output tin red as if everything is normal. but the reality is i am not capturing stdout or stderr into my own variable at all. is this a blocking way of writing subprocess or something? Any help is appreciated.


Pierce Lopez

Nov 11, 2020, 12:02:37 PM11/11/20
to python-...@googlegroups.com
It looks like you want to capture stderr along with stdout. I think it should be sufficient to add stderr=subprocess.STDOUT (see https://docs.python.org/3/library/subprocess.html#subprocess.STDOUT)

- Pierce

On Wed, Nov 11, 2020 at 4:42 AM rajde...@gmail.com <rajde...@gmail.com> wrote:
I am using the following code to run  ffmpeg in tornado:

self.__streaming_process = Subprocess(cmd, stdout=Subprocess.STREAM)

Rajdeep Rath

Nov 11, 2020, 5:44:29 PM11/11/20
to python-...@googlegroups.com
Yes, that helped a lot!! Thank you very much. In fact, i would like to add that with FFmpeg the following line does not work well in printing all the output :

line = await self.__streaming_process.stdout.read_until_regex(b"\n")

This is because after sensing the encoding parameters the delimiter changes to \r from \n just before the actual encoding starts. So you actually need to use this instead:

line = await self.__streaming_process.stdout.read_until_regex(b"\r\n|\r|\n")


You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-tornado/CAOR_4NA0SO-mj5qSerOScn2%3DEFv7UYEQf2pEANDQ%2BW4S3yRjJg%40mail.gmail.com.
Reply all
Reply to author
0 new messages