Files
EnglishNotes/真题研究/模拟卷/模拟卷一/讲解视频/build_video.py
T

51 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
"""把 8 张 slide + 8 段 wav 合成最终视频"""
import subprocess, os, imageio_ffmpeg
FFMPEG = imageio_ffmpeg.get_ffmpeg_exe()
HERE = os.path.dirname(os.path.abspath(__file__))
def run(cmd):
print(">>", " ".join(cmd[:6]), "...")
r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace")
if r.returncode != 0:
print("STDERR:", r.stderr[-1500:])
raise SystemExit(1)
# 1) 每段 slide+wav -> clipXX.mp4
for i in range(1, 9):
n = f"{i:02d}"
slide = os.path.join(HERE, f"slide{n}.png")
wav = os.path.join(HERE, f"seg{n}.wav")
clip = os.path.join(HERE, f"clip{n}.mp4")
run([
FFMPEG, "-y",
"-loop", "1", "-framerate", "30", "-i", slide,
"-i", wav,
"-c:v", "libx264", "-tune", "stillimage", "-pix_fmt", "yuv420p",
"-vf", "scale=1920:1080",
"-c:a", "aac", "-b:a", "192k", "-ar", "44100",
"-shortest",
clip,
])
print("clip", n, "done")
# 2) concat
list_file = os.path.join(HERE, "concat.txt")
with open(list_file, "w", encoding="utf-8") as f:
for i in range(1, 9):
n = f"{i:02d}"
clip = os.path.join(HERE, f"clip{n}.mp4").replace("\\", "/")
f.write(f"file '{clip}'\n")
final = os.path.join(HERE, "Margaret_Hamilton_完形精讲.mp4")
run([
FFMPEG, "-y",
"-f", "concat", "-safe", "0", "-i", list_file,
"-c:v", "libx264", "-pix_fmt", "yuv420p",
"-c:a", "aac", "-b:a", "192k",
"-movflags", "+faststart",
final,
])
print("FINAL:", final)