import os 
import time
from tqdm import tqdm 

from .frame_extract import frame_extractor
from .common import filter_json_files, cut_timestamp, convert_timestamp, get_video_duration
from .read_folder import read_images_in_order
from modules.video_processing.image_qa import image_caption
from modules.video_processing.image_qa_doubao import image_caption_doubao, image_compare_doubao
from modules.video_processing.video_cut import moviepy_cut

from .logger_config import setup_logger

logger = setup_logger(__name__)

# 1、抽帧保存
# 2、帧理解
# 3、LLM判断


def cut_once(video_path):
    # 0、任务计时
    start = time.time()

    # 1、抽帧保存：每隔60秒抽一帧
    frame_extractor(video_path, interval_sec=60)

    # 2、视频帧理解
    images = read_images_in_order("./data/key_frame/")
    image_caption_doubao(images)

    # 3、视频帧对比：寻找视频裁切时间戳
    filter_images = filter_json_files("./data/img_caption/")
    image_compare_doubao(filter_images)

    # 4、寻找视频裁切时间戳
    timestamp = cut_timestamp("./data/img_caption/for_cut/")

    # 5、长视频裁切
    clips = []
    video_duration = get_video_duration(video_path)
    timestamp_converted = convert_timestamp(timestamp, video_duration)
    clip_name = os.path.splitext(os.path.basename(video_path))[0]
    for i, cut_time in tqdm(enumerate(timestamp_converted)):
        clip_path = "./data/sub_video/" + clip_name + f"{chr(97+i)}.mp4" 
        moviepy_cut(video_path, clip_path, cut_time[0], cut_time[1])
        clips.append(clip_path)
    # return clips

    # 6、短视频裁切
    logger.info(f"first_cut: 耗时 - {time.time() - start} sec!")
    return timestamp, clips

if __name__ == "__main__":
    
    a = cut_once("data/raw_video/rawvideo.flv")
    print(a)

    # image_list = ["/data/data/luosy/project/oral/data/key_frame/frame_00000000.jpg", "/data/data/luosy/project/oral/data/key_frame/frame_00003000.jpg"]

    # image_compare_doubao(image_list)
    # TODO:parallel_fast_extractor自适应地决定抽帧次数，直至间隔1分钟抽取。