import os
import time
import json 
import subprocess
from .logger_config import setup_logger

# 设置日志
logger = setup_logger(__name__)

def get_video_duration(video_path):
    """
    获取视频时长（以秒为单位）。
    """
    command = f'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "{video_path}"'
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    return float(result.stdout.strip())

def merge_video(video_1, video_2, video_output, fade_duration=0.2):
    logger.info(f"口播视频拼接。。。")
    
    # 获取 video_1 的时长
    video_1_duration = get_video_duration(video_1)
    
    # 设置转场和淡入淡出效果
    filter_complex = f"""
        [0:v]fade=type=out:duration={fade_duration}:start_time={video_1_duration - fade_duration}[v0f];
        [1:v]fade=type=in:duration={fade_duration}:start_time=0[v1f];
        [v0f][v1f]concat=n=2:v=1:a=0[outv];
        [0:a][1:a]acrossfade=d={0.1}[outa]
    """.replace("\n", "")
    
    # 执行视频拼接
    command = f'ffmpeg -i {video_1} -i {video_2} -filter_complex "{filter_complex}" -map "[outv]" -map "[outa]" -y {video_output}'
    subprocess.run(command, shell=True)

def concat_videos(video_list):
    """
    按列表顺序拼接多个视频。

    Args:
        video_list (list): 视频文件路径列表
    """
    if len(video_list) < 2:
        logger.error("至少需要两个视频进行拼接。")
        return

    # 使用第一个视频作为初始视频
    merged_video = video_list[0]

    for idx, video in enumerate(video_list[1:], start=1):
        output_file = f'output/oral_video/merged_video_{idx}.mp4'
        merge_video(merged_video, video, output_file)
        merged_video = output_file 

    # 最终合并结果重命名为 merged_video.mp4
    video_name = os.path.splitext(os.path.basename(video_list[0]))[0].split('_')[0]
    final_output = f"output/oral_video/final-{video_name}.mp4"
    subprocess.run(f'mv {merged_video} {final_output}', shell=True)
    logger.info(f"最终合并视频保存为 {final_output}")

    return final_output


if __name__ == "__main__":
    
    input_files = [
        "./output/video_clips/test_video_003.mp4", 
        "./output/video_clips/test_video_002.mp4",
        "./output/video_clips/test_video_004.mp4",
    ]

    merge_videos(input_files)