files = [] # 遍历出所有的文件,这里没有做文件类型判断,因为视频的封装格式有好多种,就不一一列举了 for path, dir_list, file_list in os.walk(r'd:\xxx'): for a in file_list: if'~'in a or'$'in a: continue files.append(os.path.join(path, a)) for a in files: # 通过ffprobe来输出视频文件的信息 result = subprocess.Popen(f'ffprobe.exe -hide_banner -print_format json -show_streams "{a}"', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE, ) # 等待程序结束然后输出json格式的信息 out, err = result.communicate() result = json.loads(out.decode())['streams'] # 判断是否是指定的编码格式,如果不是则输出 if'"codec_long_name": "H.264'notin out.decode() and'"codec_long_name": "AAC'notin out.decode(): print(a, f'{round(os.path.getsize(a) / (1024 * 1024), 2)}M') for b in result: print(f"{b['codec_type']}:{b['codec_long_name']}")
输出示例如下:
1 2 3 4 5 6 7 8 9 10
d:\xxx\111801.36M video:Windows Media Video 9 audio:Windows Media Audio 2 d:\xxx\222978.25M video:Windows Media Video 9 audio:Windows Media Audio 2 d:\xxx\333446.29M video:Windows Media Video 9 audio:Windows Media Audio 2 d:\xxx\444905.61M
通过 ffmpeg 转换成对应的 H.264 格式。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
# -*- coding: utf-8 -*- import subprocess import time
result = subprocess.Popen(r'ffmpeg.exe -y -i d:\xxx d:\xxx.mp4', shell=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf8') # while result.poll() isNone: print(result.stdout.readline().strip()) time.sleep(0.1)