简体   繁体   English

如何在python中使用opencv同时播放多个视频?

[英]how can I play multiple videos simultaneously using opencv in python?

Hi guys I am stuck at this point. 大家好,我被困在这一点上。 I want to play four videos on my screen using opencv . 我想使用opencv在屏幕上播放四个视频。 Can anyone help me how to do that? 谁能帮我怎么做? Suppose I want to play simultaneously 假设我想同时玩

  1. first.avi first.avi
  2. second.avi second.avi
  3. third.avi Third.avi
  4. fourth.avi avi

I am referring following code. 我指的是以下代码。 It plays very well for single avi file. 对于单个avi文件,它的播放效果非常好。 Is it necessary to concatenate or i can run in four different windows?. 是否有必要串联或我可以在四个不同的窗口中运行? any suggestions are welcome import cv2 import numpy as np 任何建议都可以导入cv2 import numpy as np

# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('first.avi')
cap2 =cv2.VideoCapture('second.avi')

if (cap.isOpened()== False): 
  print("Error opening video stream or file")
if (cap2.isOpened()== False): 
  print("Error opening video stream or file")

while(cap.isOpened()||cap2.isOpened()):
  # Capture frame-by-frame
  ret, frame = cap.read()
  ret, frame1 = cap2.read()
  if ret == True:

   # Display the resulting frame
    cv2.imshow('Frame',frame)
    cv2.imshow('Frame', frame1)


   # Press Q on keyboard to  exit
   if cv2.waitKey(25) & 0xFF == ord('q'):
  break
  else: 
    break


cap.release()
cap2.release()

cv2.destroyAllWindows()

For playing multiple videos, we have to use unique window titles for each video. 要播放多个视频,我们必须为每个视频使用唯一的窗口标题。 Here is a sample code demonstrating how it can be achieved. 这是示例代码,演示如何实现。

import numpy as np
import cv2

names = ['first.avi', 'second.avi', 'third.avi', 'fourth.avi'];
window_titles = ['first', 'second', 'third', 'fourth']


cap = [cv2.VideoCapture(i) for i in names]

frames = [None] * len(names);
gray = [None] * len(names);
ret = [None] * len(names);

while True:

    for i,c in enumerate(cap):
        if c is not None:
            ret[i], frames[i] = c.read();


    for i,f in enumerate(frames):
        if ret[i] is True:
            gray[i] = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
            cv2.imshow(window_titles[i], gray[i]);

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


for c in cap:
    if c is not None:
        c.release();

cv2.destroyAllWindows()

PS: This code is just a quick and dirty example for demo purpose only. PS:这段代码只是一个简单又肮脏的示例,仅用于演示目的。 Tested with python 2 and OpenCV 3.2 on Ubuntu 14.04. 在Ubuntu 14.04上使用python 2和OpenCV 3.2进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 OpenCV、多线程、队列在 python 中同时运行多个视频? - How to run multiple videos simultaneously in python using OpenCV, multi threading, queues? 如何使用 Opencv 和 python 从 web 播放视频 - How to to play videos from the web using Opencv and python 如何在 soundcloud 上同时播放多首曲目? - How can I play multiple tracks on soundcloud simultaneously? 如何使用 OpenCV Python 一次从大量视频中提取和保存图像帧? - How can I extract and save image frames from a large number of videos all at once using OpenCV Python? 如何在Python中同时演奏两个音符? - How can I make a play two musical notes simultaneously in Python? 如何使用Winsound同时播放多个声音? - How to play multiple sounds simultaneously using winsound? 使用 openCV 时如何在 Google Colab 上播放视频? - How to play videos on Google Colab while using openCV? 使用Python和Win32,如何在不发送的情况下同时打开多封电子邮件? - Using Python and win32 how can I open multiple emails simultaneously without sending? 如何使用 python 子进程同时运行多个 .bat 文件 - How can i run multiple .bat files simultaneously using python subprocess 在python中如何将列表的多个值同时设置为零? - In python how can I set multiple values of a list to zero simultaneously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM