I am trying to pull out information about the resolution of the video clip, but it gives an error. Help me figure out where the joint is. from subprocess import Popen, PIPE import re import ffmpeg

def getvideodetails(filepath): cmd = 'ffmpeg -i %s" % filepath' p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) di = p.communicate() for line in di: if line.rfind("Video") > 0: resolution = re.findall('(\d+x\d+)', line)[0] return (resolution) getvideodetails("D:\Документы\проекты\заготовки\1.mp4") 

Gives out:

 Traceback (most recent call last): File "D:/htpthd/bin/probnik/разрешение.py", line 15, in <module> getvideodetails("D:\Документы\проекты\заготовки\1.mp4") File "D:/htpthd/bin/probnik/разрешение.py", line 10, in getvideodetails if line.rfind("Video") > 0: TypeError: argument should be integer or bytes-like object, not 'str' Process finished with exit code 1 
  • For such a task, it is better to use not ffmpeg, but ffprobe - andreymal

1 answer 1

Judging by the error TypeError: argument should be integer or bytes-like object, not 'str' , you have bytes in the line , not a line.

In rfind transfer bytes, an example:

 ... if line.rfind(b"Video") > 0: ... 

Another option would be to specify the encoding to get the result in the string:

 p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, encoding='utf-8') 
  • Now this is what it says: UnboundLocalError: local variable 'resolution' referenced before assignment - Sacred Sacred
  • one
    @SacredSacred, you have the resolution created inside the loop in the if , and then it returns from the function, but what if the if code didn't go in? That is what happened to you. Alternatively, instead of resolution = re.findall('(\d+x\d+)', line)[0] exit the function: return re.findall('(\d+x\d+)', line)[0] , and return (resolution) for example, replace return "" - gil9red