简体   繁体   中英

python subprocess with ffmpeg give no output

I m want to extract the scene change timestamp using the scene change detection from ffmpeg. I have to run it on a few hundreds of videos , so i wanted to use a python subprocess to loop over all the content of a folder. My problem is that the command that i was using for getting these values on a single video involve piping the output to a file which seems to not be an option from inside a subprocess call.

this is my code :

 p=subprocess.check_output(["ffmpeg", "-i", sourcedir+"/"+name+".mpg","-filter:v", "select='gt(scene,0.4)',showinfo\"","-f","null","-","2>","output"])

this one tell ffmpeg need an output

 output = "./result/"+name
 p=subprocess.check_output(["ffmpeg", "-i", sourcedir+"/"+name+".mpg","-filter:v", "select='gt(scene,0.4)',metadata=print:file=output","-an","-f","null","-"])

this one give me no error but doesn't create the file

this is the original command that i use directly with ffmpeg:

ffmpeg -i input.flv  -filter:v "select='gt(scene,0.4)',showinfo"  -f null  - 2> ffout

I just need the ouput of this command to be written to a file, anyone see how i could make it work? is there a better way then subprocess ? or just another way ? will it be easier in C?

You can redirect the stderr output directly from Python without any need for shell=True which can lead to shell injection.

It's as simple as:

with open(output_path, 'w') as f:
    subprocess.check_call(cmd, stderr=f)

Things are easier in your case if you use the shell argument of the subprocess command and it should behave the same. When using the shell command, you can pass in a string as the command rather then a list of args.

cmd = "ffmpeg -i {0}  -filter:v \"select='gt(scene,0.4)',showinfo\"  -f {1}  - 2> ffout".format(inputName, outputFile)
p=subprocess.check_output(cmd, shell=True)

If you want to pass arguments, you can easily format your string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM