python根據達芬奇場景分析儲存的edl檔案,智慧裁切輸出4K影片畫面(不帶聲音)

大话人生發表於2024-03-29
import cv2


#讀取切分檔案
def readQiFenWenJian(filename):
    with open(filename, "r", encoding='UTF-8')as f:
        res_list = f.readlines()
        print("讀取到的切分檔案")

        print(res_list)
        print(len(res_list))
    zuizhong_res_list = []
    for i in range(2,len(res_list)):
        if res_list[i] != '\n':
            zuizhong_res_list.append(res_list[i])

    print(zuizhong_res_list)
    print(len(zuizhong_res_list))
    return zuizhong_res_list



# 讀取一個影片檔案,從幀列表中,開始算後續幀,第一個幀為0
def handleOneVideo(video_file,zhen_list):
    for i in range(0, len(zhen_list)):
        yihang_shifenmiao = []
        yihangshuju = zhen_list[i]
        print("一行資料:")
        print(yihangshuju)
        yihang_list = yihangshuju.split(" ")
        print("yihang列表:")
        print(yihang_list)
        for one in yihang_list:
            if ":" in one:
                yihang_shifenmiao.append(one)

        print("yihang時分秒幀-列表:")
        print(yihang_shifenmiao)
        start_shifenmiao = yihang_shifenmiao[0]
        start_shifenmiao_list = start_shifenmiao.split(":")
        print("start_shifenmiao_list:")
        print(start_shifenmiao_list)

        end_shifenmiao = yihang_shifenmiao[1]
        end_shifenmiao_list = end_shifenmiao.split(":")
        print("end_shifenmiao_list:")
        print(end_shifenmiao_list)


        START_HOUR = int(start_shifenmiao_list[0])  # 開始小時
        START_MIN = int(start_shifenmiao_list[1])  # 開始分鐘
        START_SECOND = int(start_shifenmiao_list[2])  # 開始秒數
        START_TIME = START_HOUR * 3600 + START_MIN * 60 + START_SECOND  # 設定開始時間(單位秒)
        END_HOUR = int(end_shifenmiao_list[0])  # 結束小時
        END_MIN = int(end_shifenmiao_list[1])  # 結束分鐘
        END_SECOND = int(end_shifenmiao_list[2])   # 結束秒
        END_TIME = END_HOUR * 3600 + END_MIN * 60 + END_SECOND  # 設定結束時間(單位秒)

        video = video_file
        cap = cv2.VideoCapture(video)  # 讀取影片
        FPS = cap.get(cv2.CAP_PROP_FPS)
        print("幀率:")
        print(FPS)

        size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        print("大小:")
        print(size)

        TOTAL_FRAME = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))  # 獲取影片總幀數
        print("總幀數:")
        print(TOTAL_FRAME)
        frameToStart = START_TIME * FPS+int(start_shifenmiao_list[3])  # 開始幀 = 開始時間*幀率 + 多餘幀   根據達芬奇切割點自動算出開始幀
        print("開始幀:")
        print(frameToStart)
        frametoStop = END_TIME * FPS + int(end_shifenmiao_list[2])  # 結束幀 = 結束時間*幀率 + 多餘幀 根據達芬奇切割點自動算出開始幀
        print("結束幀:")
        print(frametoStop)

        cishu = i
        # 分批寫入影片檔案
        videoWriter = cv2.VideoWriter(
            r'%s_%s.mp4' % (video, cishu), cv2.VideoWriter_fourcc(*'mp4v'), FPS, size)

        cap.set(cv2.CAP_PROP_POS_FRAMES, frameToStart)  # 設定讀取的位置,從第幾幀開始讀取影片

        COUNT = frameToStart
        while True:
            success, frame = cap.read()
            if success:
                COUNT += 1
                if COUNT <= frametoStop and COUNT > frameToStart:  # 選取起始幀
                    print('correct= ', COUNT)
                    videoWriter.write(frame)
            # print('mistake= ', COUNT)
            if COUNT > frametoStop:
                break
        print('end')




if __name__ == '__main__':
    qiefenwenjian = r"C:\4k\蘇幕遮-小點.edl"
    video_file =r"C:\4k\ceshi\daichuli\蘇幕遮.mp4"
    zhen_list = readQiFenWenJian(filename=qiefenwenjian)
    handleOneVideo(video_file, zhen_list)

相關文章