Youtube영상 처리
필요한 패키지 설치
$ pip install pafy
$ pip install youtube-dl
import pafy
import cv2
url = "https://www.youtube.com/watch?v=gdZLi9oWNZg"
video = pafy.new(url)
print("video title : {}".format(video.title))
print("video rating : {}".format(video.rating))
print("video viewcount : {}".format(video.viewcount))
print("video author : {}".format(video.author))
print("video length : {}".format(video.length))
print("video duration : {}".format(video.duration))
print("video likes : {}".format(video.likes))
print("video dislikes : {}".format(video.dislikes))
best = video.getbest(preftype="mp4")
print("best resolution : {}".format(best.resolution))
cap = cv2.VideoCapture(best.url)
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frameRate = int(cap.get(cv2.CAP_PROP_FPS))
frame_size = (frameWidth, frameHeight)
print('frame_size={}'.format(frame_size))
print('fps={}'.format(frameRate))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out1Path = 'data/recode1.mp4'
out2Path = 'data/recode2.mp4'
out1 = cv2.VideoWriter(out1Path, fourcc, frameRate, frame_size)
out2 = cv2.VideoWriter(out2Path, fourcc, frameRate, frame_size)
while True:
retval, frame = cap.read()
if not(retval):
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
out1.write(frame)
out2.write(edges)
cv2.imshow('frame', frame)
cv2.imshow('edges', edges)
key = cv2.waitKey(frameRate)
if key == 27:
break
if cap.isOpened():
cap.release()
out1.release()
out2.release()
cv2.destroyAllWindows()