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)
相关推荐
无须logic ᭄6 分钟前
CrypTen项目实践
python·机器学习·密码学·同态加密
Channing Lewis19 分钟前
flask常见问答题
后端·python·flask
Channing Lewis20 分钟前
如何保护 Flask API 的安全性?
后端·python·flask
水兵没月1 小时前
钉钉群机器人设置——python版本
python·机器人·钉钉
我想学LINUX2 小时前
【2024年华为OD机试】 (A卷,100分)- 微服务的集成测试(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·微服务·集成测试
数据小爬虫@5 小时前
深入解析:使用 Python 爬虫获取苏宁商品详情
开发语言·爬虫·python
健胃消食片片片片5 小时前
Python爬虫技术:高效数据收集与深度挖掘
开发语言·爬虫·python
ℳ₯㎕ddzོꦿ࿐8 小时前
解决Python 在 Flask 开发模式下定时任务启动两次的问题
开发语言·python·flask
CodeClimb8 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
一水鉴天8 小时前
为AI聊天工具添加一个知识系统 之63 详细设计 之4:AI操作系统 之2 智能合约
开发语言·人工智能·python