当前位置: 首页 > news >正文

详细介绍:录制mp4

目录

单线程保存mp4

多线程保存mp4 rtsp

ffmpeg录制mp4


单线程保存mp4

import cv2import imageio cv2.namedWindow('photo', 0)  # 0窗口大小可以任意拖动,1自适应cv2.resizeWindow('photo', 1280, 720)url ="rtsp://admin:aa123456@192.168.1.64/h264/ch1/main/av_stream"cap = cv2.VideoCapture(1)ret = cap.isOpened()imgs = []fps =30index = 0count = 0strat_record = Falsewhile (ret):    ret, img = cap.read()    if not ret: break    cv2.imshow('photo', img)    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)    if strat_record:        imgs.append(img)    index +=1    if index %300 == 299 and strat_record:        count+=1        save_video_path = f'lanqiu_{count}.mp4'        imageio.mimsave(save_video_path, imgs, fps=fps, macro_block_size=None)        imgs=[]     key = cv2.waitKey(1) & 0xFF    if key == ord('q'):        break    elif key == ord('s'):        strat_record = True        print("start_record", strat_record)    elif key == ord('e'):        strat_record = False        print("end_record", strat_record)cap.release()save_video_path = f'lanqiu_{count}.mp4'imageio.mimsave(save_video_path, imgs, fps=fps, macro_block_size=None)

多线程保存mp4 rtsp

import cv2import threadingimport queueimport time # 参数设置url = "rtsp://admin:aa123456@192.168.1.64/h264/ch1/main/av_stream"fps = 30segment_time = 10  # 每段录制 10 秒fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 用于保存帧的队列frame_queue = queue.Queue()recording = Falsestop_signal = Falsevideo_count = 0 # 保存线程函数def save_video_worker():    global video_count    while True:        if stop_signal and frame_queue.empty():            break         frames = []        start_time = time.time()        while time.time() - start_time < segment_time:            try:                frame = frame_queue.get(timeout=1)                frames.append(frame)            except queue.Empty:                continue         if frames:            h, w = frames[0].shape[:2]            video_count += 1            save_path = f'lanqiu_{video_count}.mp4'            out = cv2.VideoWriter(save_path, fourcc, fps, (w, h))            for f in frames:                out.write(f)            out.release()            print(f"[保存完成] {save_path}") # 启动摄像头cap = cv2.VideoCapture(url)ret = cap.isOpened() cv2.namedWindow('photo', 0)cv2.resizeWindow('photo', 1280, 720) # 开启保存线程(一直运行,直到设置 stop_signal)thread = threading.Thread(target=save_video_worker)thread.start() while ret:    ret, frame = cap.read()    if not ret:        break     cv2.imshow('photo', frame)     key = cv2.waitKey(1) & 0xFF    if key == ord('q'):        break     elif key == ord('s') and not recording:        recording = True        print("[开始录制]")     elif key == ord('e') and recording:        recording = False        print("[停止录制]")     if recording:        frame_queue.put(frame.copy())  # 用 copy 避免线程间冲突 cap.release()stop_signal = Truethread.join()cv2.destroyAllWindows()

ffmpeg录制mp4

import subprocessimport threadingimport queueimport timeimport cv2import numpy as np # === 参数设置 ===rtsp_url = "rtsp://admin:aa123456@192.168.1.64/h264/ch1/main/av_stream"width, height = 1280, 720fps = 25segment_time = 10  # 每段录制时间(秒)fourcc = cv2.VideoWriter_fourcc(*'mp4v') recording = Falsestop_signal = Falsevideo_count = 0 frame_queue = queue.Queue() # === 保存线程函数 ===def save_video_worker():    global video_count    while not stop_signal or not frame_queue.empty():        frames = []        start_time = time.time()        while time.time() - start_time < segment_time:            try:                frame = frame_queue.get(timeout=1)                frames.append(frame)            except queue.Empty:                continue         if frames:            video_count += 1            out = cv2.VideoWriter(f'video_segment_{video_count}.mp4', fourcc, fps, (width, height))            for f in frames:                out.write(f)            out.release()            print(f"[保存完成] video_segment_{video_count}.mp4") # === 启动 FFmpeg 读取 RTSP ===ffmpeg_cmd = [    r'E:\soft\ffmpeg-7.1.1-full_build\ffmpeg-7.1.1-full_build\bin\ffmpeg.exe',    '-rtsp_transport', 'tcp',    '-i', rtsp_url,    '-f', 'rawvideo',    '-pix_fmt', 'bgr24',    '-vf', f'scale={width}:{height}',    '-'] pipe = subprocess.Popen(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=10**8) # === 启动保存线程 ===thread = threading.Thread(target=save_video_worker)thread.start() # === 实时显示和按键控制 ===cv2.namedWindow("photo", 0)cv2.resizeWindow("photo", width, height) try:    while True:        raw_frame = pipe.stdout.read(width * height * 3)        if not raw_frame:            print("视频读取失败,退出")            break         frame = np.frombuffer(raw_frame, np.uint8).reshape((height, width, 3))        cv2.imshow("photo", frame)         key = cv2.waitKey(1) & 0xFF        if key == ord('q'):            break        elif key == ord('s') and not recording:            recording = True            print("[开始录制]")        elif key == ord('e') and recording:            recording = False            print("[停止录制]")         if recording:            frame_queue.put(frame.copy()) except KeyboardInterrupt:    print("中断退出") # === 清理资源 ===stop_signal = Truethread.join()pipe.terminate()cv2.destroyAllWindows()

http://www.hskmm.com/?act=detail&tid=26848

相关文章:

  • 10月8日
  • 【OpenGL ES】光栅化插值原理和射线拾取原理
  • HTML 速查列表 - 教程
  • Exp1
  • 20_uv_wsl_installation
  • 学习问题日记-4
  • Codeforces Round 1042 (CF2131) 补题笔记(A-E)
  • 在AI技术唾手可得的时代,挖掘新需求成为核心竞争力——某知名AI编程助手框架需求探索
  • 表格数据自动机器学习技术解析
  • 10/8
  • 2025.10.8
  • 【QT】QString 与QString区别 - 教程
  • 连通分量tarjan学习笔记
  • [Python/地图] 基于Python绘制地图
  • 实验任务1——8
  • 一款专门为 WPF 打造的开源 Office 风格用户界面控件库
  • dockercontainerd代理设置脚本
  • 实用指南:vue3+elementplus表格表头加图标及文字提示
  • 2025国庆集训总结
  • tampermonkey油猴脚本, 动画疯评分显示增强脚本
  • 9.29课后整理 - GENGAR
  • 深入解析:【QT】`QTextCursor::insertText()`中插入彩色文本
  • Java方法专题 - 动手动脑问题与实验总结
  • 2025年中盘点
  • 学习问题日记-3
  • 差分约束乘法改加减
  • 01-方法-课后作业
  • 应用程序io接口
  • 边缘数据库近期想法(2)
  • 方法-课后作业1