-
Notifications
You must be signed in to change notification settings - Fork 27
/
pytubeTest.py
52 lines (38 loc) · 1.74 KB
/
pytubeTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import glob
import os
from contextlib import closing
from videosequence import VideoSequence
from PIL import Image
import face_recognition
import subprocess
from pytube import YouTube
folderNumber = "3"
if not os.path.exists(folderNumber):
os.makedirs(folderNumber)
YouTube('https://www.youtube.com/watch?v=0bdHdGS3OlI').streams.first().download(folderNumber)
list_of_files = glob.glob(folderNumber+'/*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
folderName = folderNumber+"/origImages"
faceFolderName = folderNumber+"/faceImages"
if not os.path.exists(folderName):
os.makedirs(folderName)
if not os.path.exists(faceFolderName):
os.makedirs(faceFolderName)
with closing(VideoSequence(latest_file)) as frames:
for idx, frame in enumerate(frames[:]):
filename = folderName+"/"+"frame{:04d}.jpg".format(idx)
frame.save(filename)
# Load the jpg file into a numpy array
image = face_recognition.load_image_file(filename)
# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)
if(len(face_locations) == 1):
top, right, bottom, left = face_locations[0]
faceFilename = faceFolderName+"/"+"frame{:04d}.jpg".format(idx)
height = top-bottom
faceFrame = frame.crop((left,top,right,bottom-height*0.3))
faceFrame.save(faceFilename)
command = "ffmpeg -i "+latest_file+" -ab 160k -ac 2 -ar 44100 -vn "+folderNumber+"/audio.wav"
subprocess.call(command, shell=True)