import os
import json
import time
import subprocess
from tqdm import tqdm 
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from utils.caption import generate_srt_file, burn_subtitles_ffmpeg
from utils.logger_config import setup_logger

logger = setup_logger(__name__)

# # 视频裁切-ffmpeg
# def ffmpeg_cut(input_path, output_path, start_time, end_time):
#     cmd = [
#         'ffmpeg',
#         '-hwaccel', 'cuvid',        # 启用NVIDIA GPU加速（可选）
#         '-i', input_path,
#         '-ss', str(start_time),
#         '-to', str(end_time),
#         '-c:v', 'h264_nvenc',       # 使用NVIDIA硬件编码（可选）
#         '-c:a', 'copy',
#         '-c:v', 'copy',             # 关键：避免重新编码，直接复制视频流
#         output_path
#     ]
#     subprocess.run(cmd, check=True)

# # 视频裁切-ffmpeg
# def cut_video_ffmpeg(input_path, output_path, start_time, duration):
#     command = [
#         'ffmpeg',
#         '-ss', str(start_time),  # 开始时间（格式：00:00:00 或秒数）
#         '-i', input_path,
#         '-t', str(duration),     # 持续时间
#         '-c', 'copy',            # 直接复制流，不重新编码
#         '-y',                    # 覆盖输出文件
#         output_path
#     ]
#     subprocess.run(command, check=True)

# 视频裁切-moviepy
def moviepy_cut(input_path, output_path, start_time, end_time):
    """
        start_time: xxx sec
        end_time: xxx secs
    """
    logger.info(f"视频裁切中：{output_path} - start: {start_time} - end: {end_time}")
    ffmpeg_extract_subclip(input_path, start_time, end_time, targetname=output_path)

def video_cut_by_json(json_path, add_caption=False):

    # 读取JSON文件
    try:
        with open(json_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
    except FileNotFoundError:
        print(f"错误: 文件 '{json_path}' 不存在")
        raise

    # 获取原视频路径与视频文件名
    raw_video_path = data["raw_video"]
    raw_video_name = os.path.splitext(os.path.basename(raw_video_path))[0]

    # 读取视频片段元数据，进行：视频片段裁切、SRT文件生成、视频加字幕
    clips_meta = data["oral_dict_list"]
    for i, clip_meta in tqdm(enumerate(clips_meta)):

        # 0、去除timestamp字段
        clip_meta.pop("timestamp", None)

        # 1、提取语句内容，开始时间戳，结束时间戳
        clip_text, clip_start_time, clip_end_time = clip_meta["text"], clip_meta["start"] / 1000, clip_meta["end"] / 1000

        # 2、裁剪视频片段
        clip_name = clip_meta["clip_name"]
        clip_path = "./data/clip_video/" + clip_name
        moviepy_cut(raw_video_path, clip_path, clip_start_time, clip_end_time)

        # 海外线上业务不添加字幕；国内线上业务添加字幕
        if add_caption:
            # 3、生成字幕SRT文件
            srt_list = [(0.0, (clip_end_time - clip_start_time), clip_text)]
            srt_path = "./data/clip_srt/" + clip_name
            generate_srt_file(srt_list, srt_path)

            # 4、视频添加字幕
            output_path = "./output/video_clips/" + clip_name
            burn_subtitles_ffmpeg(clip_path, srt_path, output_path, style_config=None)
        else:
            pass


if __name__ == "__main__":

    json_path = "new_transcript.json"
    video_cut_by_json(json_path)


