[原]通过配合ffmpeg.exe获取视频文件时长

时间:2022-08-17 15:35:45
import subprocess
import os
import time def getTime(flvpath,fid):
#file_str = '1.flv'
file_str = flvpath
wg = subprocess.Popen(['ffmpeg.exe', '-i', file_str], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(standardout, junk) = wg.communicate()
ans = str(standardout)
num = ans.find("Duration:")
out = ans[num+10:num+18]
fid.write(file_str + "<| time is |>" + out)
fid.write("\r\n") def getFileName(path, warp):
#f_list = os.listdir(path)
#rootdir = os.getcwd()
rootdir = path
logname = warp[1:] + '_log.txt'
fid = open(logname, 'w')
for (dirpath, dirnames, filenames) in os.walk(rootdir):
for filename in filenames:
pathname = os.path.join(dirpath, filename)
if os.path.splitext(pathname)[1].lower() == warp:
getTime(pathname,fid) fid.close() if __name__ == "__main__":
print("Begin time: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
rootdir = "D:\\mooc\\视频资料"
warp = ".flv"
getFileName(rootdir, warp)
warp = ".mp4"
getFileName(rootdir, warp)
print("End time:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))