1

am looking devolop something simply with opencv.Am looking to play videos from the web using opencv the way you can play videos from your laptop by passing the location path now a trying to pass the video url and get it to play.so far am getting error any suggestion would be nice.

import cv2 
import numpy as np 
import urllib3


http = urllib3.PoolManager()
r = http.request('Get','https://www.youtube.com/watch?v=NWdrO4BoCu8&list=RDNWdrO4BoCu8&start_radio=1')
cap = cv2.VideoCapture('https://www.youtube.com/watch?v=NWdrO4BoCu8&list=RDNWdrO4BoCu8&start_radio=1') 


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


while(cap.isOpened()): 
    

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

        cv2.imshow('Frame', frame) 

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


cap.release() 
cv2.destroyAllWindows() 
Ahmet
  • 7,527
  • 3
  • 23
  • 47
user14032379
  • 11
  • 1
  • 2

1 Answers1

0

Here you can use pafy to download the video, then use OpenCV to play the video.

url = 'https://youtu.be/W1yKqFZ34y4'
vPafy = pafy.new(url)
play = vPafy.getbest(preftype="webm")

#start the video
cap = cv2.VideoCapture(play.url)
while (True):
    ret,frame = cap.read()
    """
    your code here
    """
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break    

cap.release()
cv2.destroyAllWindows()

resources for installation:

https://pypi.org/project/pafy/

RVACode
  • 108
  • 4
  • The question is not asking **how to play video on your pc?** the answer is already available in the [tutorial](https://www.learnopencv.com/read-write-and-display-a-video-using-opencv-cpp-python/). The question asks you to is there any way to play a youtube video? – Ahmet Aug 01 '20 at 22:11
  • @AhmetTavli I apologize, I misread the question. I have updated my answer. – RVACode Aug 02 '20 at 05:54
  • No problem, no problem for me. just a reminder: `pafy` library is designed for `python2.7`. You can't install it for `python3.7` – Ahmet Aug 02 '20 at 05:57