I am trying to read a video with OpenCV after I downloaded with youtube-dl, all this on conda enviroment. But always it shows me " Error opening video stream or file". After of looking for a solution, I found many, i.e. ffmpeg support is not available then I tried but it does not work. Maybe there is other solution to read video from OpenCV, I am working with conda enviroment.
##Code to download video from youtube from __future__ import unicode_literals
import cv2
import youtube_dl
def my_hook(d):
if d['status'] == 'finished':
print('Done downloading, now converting ...')
ydl_opts = {
'format': 'bestvideo/best',
# 'outtmpl': '%(id)s'+'.avi',
'noplaylist' : True,
'progress_hooks': [my_hook],
# 'vcodec': 'avi',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=668nUCeBHyY'])
##Code to read video with OpenCV import 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('668.mp4')
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video stream or file")
count = 0
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
cv2.imwrite("frame%d.jpg" % count, frame) # save frame as JPEG file
# Display the resulting frame
cv2.imshow('Frame',frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
count = count + 1
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()