How to determine the resolution of the video using Python?

You want to determine the video resolution of the video before to send it for processing. How to do it using Python?
March 12th 20 at 08:09
2 answers
March 12th 20 at 08:11
Solution
import cv2
file_path = "./video.avi"
vid = cv2.VideoCapture( file_path )
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
March 12th 20 at 08:13
from subprocess import Popen, PIPE
import re

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("path to video")
avconv is deprecated

fmpeg - corine commented on March 12th 20 at 08:16
@corine, And if the second Python - lempi commented on March 12th 20 at 08:19
@lempi, a second Python is not used for writing new software
From Vesi Python does not depend on which version of ffmpeg'a installed on this server - corine commented on March 12th 20 at 08:22
@corine, tada Well, as I understand it - 'ffmpeg -i %s" % filepath'? - lempi commented on March 12th 20 at 08:25
@lempi, + - corine commented on March 12th 20 at 08:28
@lempi, Version python3. I tried to run and got the error TypeError: a bytes-like object is required, not 'str' . Where what to correct?
from subprocess import Popen, PIPE
import re

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")
- Ara.Muller34 commented on March 12th 20 at 08:31

Find more questions by tags VideoPython