Python3.6_x86通过FFpmeg获取视频或音频的时长和分辨率

时间:2022-04-14 10:25:05

前言

前段时间公司在做流媒体服务,与许多厂家合作拿了许多视频过来,现在要对这些视频文件进行整理,通过特殊的编码排列,获取他们的时长以及分辨率,这里我遇到一个大坑,请往下面看。

 # -*- coding: utf-8 -*-
import os
import sys
import shutil
import subprocess
import json
def getLength(filename):
command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
out = result.stdout.read()
#print(str(out))
temp = str(out.decode('utf-8'))
try:
data = json.loads(temp)['streams'][1]['width']
except:
data = json.loads(temp)['streams'][0]['width']
return data def getLenTime(filename):
command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
out = result.stdout.read()
#print(str(out))
temp = str(out.decode('utf-8'))
data = json.loads(temp)["format"]['duration']
return data

只需要传入文件的完整路径就可以获取到分辨率以及播放时长。

本次使用ffpmeg发现,使用的python3.6 ,32位和64位通过 subprocess.Popen 获取到的值是不一样的,导致后面的方法也稍微不一样,如果你是32位的,使用我上面代码无法获取

out = result.stdout
data = json.loads(out)["format"]['duration']
 
作者感言:重要是事情说三遍 路径全英文,路径全英文,路径全英文!!!!!