57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
import subprocess
|
||
from pathlib import Path
|
||
|
||
|
||
def convert_to_h264(input_path: str, output_path: str = None) -> str:
|
||
"""
|
||
将视频文件转码为 H.264 编码格式
|
||
|
||
Args:
|
||
input_path: 输入视频文件路径(相对或绝对路径)
|
||
output_path: 输出视频文件路径(可选,默认为输入文件同名_h264.mp4)
|
||
|
||
Returns:
|
||
转码后的视频文件路径
|
||
|
||
Raises:
|
||
FileNotFoundError: 输入文件不存在
|
||
subprocess.CalledProcessError: FFmpeg 转码失败
|
||
"""
|
||
input_path_all = Path("input/" + input_path).resolve()
|
||
output_path_all = Path("vid_h264/" + input_path).resolve()
|
||
|
||
if not input_path_all.exists():
|
||
raise FileNotFoundError(f"输入文件不存在:{input_path}")
|
||
|
||
print(f"开始转码:{input_path_all}")
|
||
# if output_path is None:
|
||
output_path = str(output_path_all.with_name(f"{input_path_all.stem}_h264.mp4"))
|
||
# else:
|
||
# output_path = Path(output_path).resolve()
|
||
|
||
cmd = [
|
||
"ffmpeg",
|
||
"-i", str(input_path_all),
|
||
"-c:v", "h264_nvenc", # NVIDIA 硬件编码
|
||
"-c:a", "aac",
|
||
"-b:a", "128k",
|
||
"-y",
|
||
str(output_path)
|
||
]
|
||
|
||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||
|
||
if result.returncode != 0:
|
||
raise subprocess.CalledProcessError(
|
||
result.returncode,
|
||
cmd,
|
||
result.stdout,
|
||
result.stderr
|
||
)
|
||
print(f"转码完成:{output_path}")
|
||
return str(output_path)
|
||
|
||
if __name__ == "__main__":
|
||
convert_to_h264("VID_20251104_085655_024.AVI", "vid_h264/VID_20251104_085655_024_h264.mp4")
|
||
# convert_to_h264("input/short.AVI", "vid_h264/short_h264.mp4")
|