from moviepy.editor import VideoFileClip

def zoom_and_crop_video(input_path, output_path, zoom_factor):
    """
    放大视频画面并裁剪中心区域，保持原分辨率
    :param input_path: 输入视频路径
    :param output_path: 输出视频路径
    :param zoom_factor: 缩放倍数（需≥1）
    """
    # 加载视频并获取原始尺寸
    clip = VideoFileClip(input_path)
    original_width, original_height = clip.size
    
    if zoom_factor < 1:
        raise ValueError("缩放倍数必须≥1，否则无法裁剪出原始分辨率")
    
    # 放大视频
    zoomed_clip = clip.resize(zoom_factor)
    zoomed_width, zoomed_height = zoomed_clip.size
    
    # 计算裁剪区域中心坐标
    x_center = zoomed_width // 2
    y_center = zoomed_height // 2
    half_w = original_width // 2
    half_h = original_height // 2
    
    # 确定裁剪范围（确保不越界）
    x1 = max(0, x_center - half_w)
    y1 = max(0, y_center - half_h)
    x2 = min(zoomed_width, x_center + half_w)
    y2 = min(zoomed_height, y_center + half_h)
    
    # 裁剪并保存
    cropped_clip = zoomed_clip.crop(x1=x1, y1=y1, x2=x2, y2=y2)
    cropped_clip.write_videofile(
        output_path,
        codec='libx264',
        audio_codec='aac',
        bitrate='8000k'  # 提高码率以保持清晰度[[20]]
    )
    
    # 释放资源
    clip.close()
    zoomed_clip.close()
    cropped_clip.close()





if __name__ == "__main__":
    input_path = "oral_demo.mp4"
    output_path = "oral_demo_enlarge.mp4"
    scale_factor = 1.5

    # 调用示例
    zoom_and_crop_video(input_path, output_path, scale_factor)