转换视频格式|压缩视频|使用ffmpeg
代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os.path import subprocess def compress_video(origin_file=None, target_file=None, quality=10): # 压缩质量(值越小,视频越小) # 如果文件存在就删除 if os.path.exists(target_file): os.remove(target_file) if not os.path.exists(os.path.join(os.getcwd(), "ffmpeg.exe")): return "ffmpeg程序不存在请下在该程序" # 定义FFmpeg命令 command = f'ffmpeg -i "{origin_file}" -qscale:v {quality} -strict -2 "{target_file}"' # 调用FFmpeg命令 output = subprocess.getoutput(command) # 输出FFmpeg命令执行结果 print(output) # 输入视频文件路径 origin_file = r'video.avi' # 输出视频文件路径 target_file = r'out.mp4' compress_video(origin_file=origin_file, target_file=target_file)
上图目录结构
-----------------------------------------------------------------------------------------------------------------------------------------