使用ffmpeg对mp4视频进行二次压缩

可以使用如下代码对mp4文件进行压缩来减少其大小:

#encoding:utf-8

import subprocess
from pathlib import Path
from glob import glob

directory = "."
filenames = glob("{}/*.mp4".format(directory))
output_directory = "output"
p = Path(output_directory)
if not p.exists():
	p.mkdir()
for f in filenames:
	name = Path(f).name
	out_name = p.joinpath(name)
	cmd = ["ffmpeg","-loglevel","quiet","-hide_banner","-i",f,"-c:v","libx264","-preset","veryslow","-crf","28","-c:a","copy",out_name]
	print("File:{}".format(f))
	process = subprocess.Popen(cmd)
	process.communicate()

其中-loglevel设置为安静模式将输出信息忽略,而-crf控制视频压缩质量。

参考文章:

https://www.cnblogs.com/shandianchengzi/p/18155344

posted @ 2025-02-08 09:20  月薪几千的牛马  阅读(71)  评论(0)    收藏  举报