대화형 모드 설정(interactive-on / interactive-off)
import cv2
import matplotlib.pyplot as plt
import pafy
url = 'https://www.youtube.com/watch?v=u_Q7Dkl7AIk'
video = pafy.new(url)
print('title = ', video.title)
print('video.rating = ', video.rating)
print('video.duration = ', video.duration)
best = video.getbest(preftype='mp4')
print('best.resolution', best.resolution)
cap=cv2.VideoCapture(best.url)
def handle_key_press(event):
if event.key == 'escape':
cap.release()
plt.close()
def handle_close(evt):
print('Close figure!')
cap.release()
plt.ion()
fig = plt.figure(figsize=(10, 6))
plt.axis('off')
fig.canvas.set_window_title('Video Capture')
fig.canvas.mpl_connect('key_press_event', handle_key_press)
fig.canvas.mpl_connect('close_event', handle_close)
retval, frame = cap.read()
im = plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
while True:
retval, frame = cap.read()
if not retval:
break
im.set_array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
fig.canvas.draw()
fig.canvas.flush_events()
if cap.isOpened():
cap.release()