python opencv 持续点选开始帧,结束帧,切割视频成几个小段

复制代码
import os

import cv2
import time



def on_mouse(event,x,y, flag, para):
    global status_value, start_frame, end_frame, times
    if event == cv2.EVENT_LBUTTONDOWN:  # 鼠标左键点击
        times += 1

        status_value = not status_value
        if status_value:
            start_frame = frame_number
            # print(f"Start frame: {start_frame}")
        else:
            split_flag = 'd' + str(int(times/2))
            # print('split_flag: ', split_flag)
            end_frame = frame_number
            # print(f"End frame: {end_frame}")
            extract_and_save_video(split_flag,para[0], para[1])


def extract_and_save_video(split_flag, folder, video_name):
    now_time = time.time()
    print('now_time:', now_time)
    out = cv2.VideoWriter('{}_{}_{}.avi'.format(video_name, folder, split_flag), cv2.VideoWriter.fourcc(*'XVID'), fps, size)
    print('start frame: {}'.format(start_frame))
    print('end frame: {}'.format(end_frame))
    for i in range(start_frame, end_frame + 1):
        cap.set(cv2.CAP_PROP_POS_FRAMES, i)
        ret, frame = cap.read()
        if ret:
            out.write(frame)
    out.release()
    print('video saved *********************')
    cv2.waitKey(5000)


videos_folder = 'E:\\河南深丛信息科技\\datasets\\record_night'

action_folders = os.listdir(videos_folder)[:2]
for folder in action_folders:
    video_folder = os.path.join(videos_folder, folder)
    video_files = os.listdir(video_folder)
    for video_file in video_files:
        video_name = video_file.split('.mkv')[0]
        video_file_path = os.path.join(video_folder, video_file)
        print('video file path:', video_file_path)
        cap = cv2.VideoCapture(video_file_path)
        fps = cap.get(cv2.CAP_PROP_FPS)
        print('fps: {}'.format(fps))
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        print("frame_count:", frame_count)

        status_value = 0
        # 初始化变量
        start_frame = None
        end_frame = None
        frame_number = 0
        times = 0
        event = 0

        size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

        while True:
            ret, frame = cap.read()
            if not ret:
                break

            frame_number += 1

            cv2.imshow('Video', frame)

            cv2.setMouseCallback('Video', on_mouse, [folder, video_name])

            if frame_number == 1:
                cv2.waitKey(3000)

            if cv2.waitKey(50) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()
        print(video_file + '  is done......\n')
        cv2.waitKey(3000)
相关推荐
m0_734949793 分钟前
Redis如何降低快照对CPU的影响_合理分配RDB执行时机避开业务高峰期
jvm·数据库·python
Dxy12393102168 分钟前
Python在图片上画圆形:从入门到实战
开发语言·python
小江的记录本9 分钟前
【系统设计】《2026高频经典系统设计题》(秒杀系统、短链接系统、订单系统、支付系统、IM系统、RAG系统设计)(完整版)
java·后端·python·安全·设计模式·架构·系统架构
m0_3776182318 分钟前
HTML怎么显示速率限制重置时间_HTML X-RateLimit-Reset解析【说明】
jvm·数据库·python
u01091476025 分钟前
C#怎么实现OAuth2.0授权_C#如何对接第三方快捷登录【核心】
jvm·数据库·python
2301_7775993729 分钟前
如何显著提升 Google Sheets 数据库批量更新脚本的执行效率
jvm·数据库·python
2201_7610405937 分钟前
bootstrap怎么给div添加自定义的边框样式
jvm·数据库·python
Java后端的Ai之路39 分钟前
当大模型开始“水土不服“:从通才到专才的进化论——Fine-tuning 企业级实战全攻略
人工智能·python·langchain·rag·lcel
weixin_5689960642 分钟前
Golang怎么用K8s Job执行一次性任务_Golang如何用Job资源运行批处理和迁移任务【操作】
jvm·数据库·python
耿雨飞1 小时前
Python 后端开发技术博客专栏 | 第 09 篇 GIL 深度解析与并发编程实战 -- 多线程、多进程、协程的选型
开发语言·python