2

I have the following command and I execute it in command prompt:

ffmpeg.exe -i input.mkv -i output.mkv -lavfi psnr=stats_file=psnrmkv.txt -f null -

this command computes PSNR between two files and saves the result in pnsrmkv.txt but I have to enter this command for each video and I have more than 200 videos:(. now I am trying to execute it in python. each time I have to change the name of the input and output video and PSNR text file. is it any way to implement a command line in python? could you please help me with this issue? I am a beginner and if you have any code or link to explain sth like this, I will be really thankful. I am using windows and python 3.7. can subprocess help with this problem?

I used the following code, but it produces the error:

import subprocess as s
s.call(["command.exe", "ffmpeg.exe -i E:\mpeg_file\input.mkv -i E:\mpeg_file\2.mkv -lavfi psnr=stats_file=psnrmkv.txt -f null -"])

the error:

Traceback (most recent call last):

File "", line 2, in s.call(["command.exe", "ffmpeg.exe -i E:\mpeg_file\input.mkv -i E:\mpeg_file\2.mkv -lavfi psnr=stats_file=psnrmkv.txt -f null -"])

File "D:\software\Anaconda3\envs\py37\lib\subprocess.py", line 287, in call with Popen(*popenargs, **kwargs) as p:

File "D:\software\Anaconda3\envs\py37\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 105, in init super(SubprocessPopen, self).init(*args, **kwargs)

File "D:\software\Anaconda3\envs\py37\lib\subprocess.py", line 729, in init restore_signals, start_new_session)

File "D:\software\Anaconda3\envs\py37\lib\subprocess.py", line 1017, in _execute_child startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified

david
  • 1,255
  • 4
  • 13
  • 26
  • You could use subprocess.call() to exceute shell commands. This link might help https://www.digitalocean.com/community/tutorials/how-to-use-subprocess-to-run-external-programs-in-python-3 – pudup May 03 '21 at 09:11
  • I was unable to find a duplicate for such a basic question; the duplicate I found discusses a slightly more complex scenario, but you should be able to find from it a good example of how to do what you are asking. Perhaps see also my top-voted answer, though that is primarily for Linux; nevertheless, much of it applies to Windows, too. – tripleee May 03 '21 at 09:14
  • I wrote a small code that I add it above and it produces the error, do you know what is the problem? – david May 03 '21 at 09:39
  • Why do you want to run `command.exe`? Clearly no such binary exists in your `PATH`. Just `subprocess.call(['ffmpeg.exe', '-i', r'E:\mpeg_file\input.mkv', '-i', r'E:\mpeg_file\2.mkv', '-lavfi', 'psnr=stats_file=psnrmkv.txt', '-f', 'null', '-])`; notice also the `r'...'` prefix on strings with backslashes in them (though using a forward slash instead is easier and avoids this problem). – tripleee May 03 '21 at 10:06
  • I added a couple of tangential duplicates. – tripleee May 03 '21 at 10:08
  • I used your code and it produce output 0, but I need to save the file in specific location such as E:\mpeg_file\psnrmkv.txt, but when I used this code subprocess.call(['ffmpeg.exe', '-i', r'E:\mpeg_file\input.mkv', '-i', r'E:\mpeg_file\2.mkv', '-lavfi', 'psnr=stats_file=' r'E:\mpeg_file\psnrmkv2.txt', '-f', 'null', '-']) it could not produce the output and the output is 1. do you know why? – david May 03 '21 at 11:17

0 Answers0