0

I need to read all videos in a folder and then compare each video with its original type and after calculating the PSNR, save the PSNR text file. for this aim I used the following code:

subprocess.call(['ffmpeg.exe', '-i', {i}, '-i', {o}, '-lavfi', 'psnr=stats_file=''{n}', '-f', 'null', '-'])

I enter the name of input and output file as variables {i} and {o} that each time can be a different video file, I also need to save the related PSNR text file, for this aim I define n(n=psnrmkv.txt'). but it could not produce psnrmkv.txt. do you have any suggestions? what is the problem?

I wrote the following code for read all mp4 file in a folder and calculating the PSNR:

import subprocess
import os 
import numpy as np
import scipy.io as sio
import cv2

os.chdir("E:/mpeg_file/") #change directory to downloads folder

suffix =".mp4"    #variable holdinng the .mp4 tag
fnames = os.listdir('.')  #looks at all files
# files = [x for x in os.listdir() if x.endswith(".mp4")]

files =[]  #an empty array that will hold the names of our mp3 files
i=0
for fname in fnames:  
    if fname.endswith(suffix):
        pname = os.path.abspath(fname)
        n="psnr"
        n=n+str(i)+".txt"
        subprocess.call(['ffmpeg.exe', '-i', pname, '-i', pname, '-lavfi', f'psnr=stats_file={n}', '-f', 'null', '-'])
        i=i+1   
        files.append(pname)  #add the mp3 files to our array
print (files)

but it does not produce any psnr text file!! could you please tell me what is the problem? it runs completely, but I do not have any outputs. I also need to add the location of saving the psnr text file in subprocess.

david
  • 1,255
  • 4
  • 13
  • 26
  • 1
    Just omit the braces, assuming those are string variables. – tripleee May 03 '21 at 11:47
  • 1
    Funny how I anticipated this question and already added this as a duplicate to your previous question – tripleee May 03 '21 at 11:47
  • Oh for the `n` case you want `f'psnr=stats_file={n}'` with an `f'...'` string, or `'psnr=stats_file=%s' % n`. The resulting string should not contain any quotes around the value of `n`; those are necessary and useful when you are passing the parameter to the shell, but here, you are not. – tripleee May 03 '21 at 11:49
  • Thank you very much tripleee, I wanted to ask this in the previous post, but I thought it is a new question and I have to make a new question. – david May 03 '21 at 11:55
  • That's fine _per se;_ but probably try to search before asking. The search engine on the site is somewhat finicky so many people find it more convenient to use Google or Duck Duck Go with `site:stackoverflow.com` as a search term. – tripleee May 03 '21 at 12:00
  • if I want to save the psnr.txt in a special location, what should I do? in the above code we only have psnr.txt if I want to save this file in for example E:/mpegfile what should I do? because when I use n='E:/mpeg_file/psnrmkv3.txt' it can not produce the output . I add my new code above. – david May 03 '21 at 12:41
  • If you want `psnr=stats_file=e:/mpeg_file/psnmrkv.txt` and the thing after the last `=` comes from the variable `n`, that's `f'psnr=stats_file={n}'` or equivalently `'psnr=stats_file=%s' % n` exactly as above. `n` is just a string; manipulate it just like you would any other string. – tripleee May 03 '21 at 12:45

0 Answers0