subprocess call ffmpeg (command line)

前端 未结 4 640
轻奢々
轻奢々 2020-12-31 11:16

I have been incorporating subprocess calls in my program. I have had no issues with subprocess calls for other commands, but I am having trouble getting the command line inp

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 11:49

    When you use subprocess, your command must either be a string that looks exactly like what you would type on the command line (and you set shell=True), or a list where each command is an item in the list (and you take the default shell=False). In either case, you have to deal with the variable part of the string. For instance, the operating system has no idea what "%03d" is, you have to fill it in.

    I can't tell from your question exactly what the parameters are, but lets assume you want to convert frame 3, it would look something like this in a string:

    my_frame = 3
    subprocess.call(
        'ffmpeg -r 10 -i frame%03d.png -r ntsc movie%03d.mpg' % (my_frame, my_frame),
        shell=True)
    

    Its kinda subtle in this example, but that's risky. Suppose these things were in a directory whose name name had spaces (e.g., ./My Movies/Scary Movie). The shell would be confused by those spaces.

    So, you can put it into a list and avoid the problem

    my_frame = 3
    subprocess.call(['ffmpeg', '-r', '10', '-i', 'frame%03d.png' % my_frame,
        ['-r',  'ntsc', 'movie%03d.mpg' % my_frame])
    

    More typing, but safer.

提交回复
热议问题