kkroening/ffmpeg-python

Is there a way to convert a command line to API commands?

Raketemensch opened this issue · 2 comments

I've been using this command line, which is pretty gnarly:

-c:v:0 libx264 -crf 23 -x264-params keyint=50:min-keyint=25:scenecut=-1 -maxrate:0 1300k #-bufsize:0 2600k -preset faster -tune zerolatency -profile:v Main -level 3.1 -c:a:0 aac #-ar:0 44100 -b:a:0 128k -flags +global_header -f flv rtmp://192.168.1.6:1935/live/abc123

Is there a simple way to convert this to ffmpeg-python syntax?

you should be able to just use the parameters as kwargs.

stream = ffmpeg.input("input.mp4")
stream = ffmpeg.output(stream, 'rtmp://192.168.1.6:1935/live/abc123', **{'c:v:0': 'libx264', 'crf': 23, 'x264-params': 'keyint=50:min-keyint=25:scenecut=-1', 'maxrate:0': '1300k' , 'bufsize:0': '2600k', 'preset': 'faster', 'tune': 'zerolatency', 'profile:v': 'Main', 'level': '3.1', 'c:a:0': 'aac', 'ar:0': '44100', 'b:a:0': '128k', 'flags': '+global_header', 'f': 'flv'})
stream.run()

this would turn into

ffmpeg -i input.mp4 -f flv -ar:0 44100 -b:a:0 128k -bufsize:0 2600k -c:a:0 aac -c:v:0 libx264 -crf 23 -flags +global_header -level 3.1 -maxrate:0 1300k -preset faster -profile:v Main -tune zerolatency -x264-params keyint=50:min-keyint=25:scenecut=-1 rtmp://192.168.1.6:1935/live/abc123

Edit: if you want to automate that, given a command in the format you provided you can use this:

command = "-c:v:0 libx264 -crf 23 -x264-params keyint=50:min-keyint=25:scenecut=-1 -maxrate:0 1300k -bufsize:0 2600k -preset faster -tune zerolatency -profile:v Main -level 3.1 -c:a:0 aac -ar:0 44100 -b:a:0 128k -flags +global_header -f flv rtmp://192.168.1.6:1935/live/abc123"
command = command.split(" ")
output = command.pop(-1)
params = {command[i].replace("-", "", 1):command[i+1] for i in range(0,len(command),2)}

stream = ffmpeg.input('test.mp4')
stream = ffmpeg.output(stream, output, **params)
stream.run()

though do note that this wouldn't work if there were spaces in any of the parameters

Thanks! This connected right up and started streaming.

I'm getting weird pauses, and the stream drops between files, I'll still have to sort those issues. I've been using ffplayout to feed owncast for a few weeks now, so I know my platform is stable, I just need to figure out why the stuttering is happening and how to better switch between files.