-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube_down.py
102 lines (73 loc) · 3.59 KB
/
youtube_down.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''
cmd에서도 가능
> youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=4J5cKU5IUAI&t=4678s
> youtube-dl -f bestaudio https://www.youtube.com/watch?v=4J5cKU5IUAI&t=4678s --output "out.%(ext)s"
> youtube-dl -x --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch?v=4J5cKU5IUAI&t=4678s
't' is not recognized as an internal or external command, operable program or batch file. --> 관리자권한으로 실행해야 한다.
'''
import os
#import youtube_dl # pip install youtube-dl ---> 버전 바뀜 pip install yt-dlp
from yt_dlp import YoutubeDL
VIDEO_DOWNLOAD_PATH = './' # 다운로드 경로
def download_video_and_subtitle(output_dir, youtube_video_list):
download_path = os.path.join(output_dir, '%(id)s-%(title)s.%(ext)s')
for video_url in youtube_video_list:
# youtube_dl options
ydl_opts = {
'format': 'best/best', # 가장 좋은 화질로 선택(화질을 선택하여 다운로드 가능)
'outtmpl': download_path, # 다운로드 경로 설정
'writesubtitles': 'best', # 자막 다운로드(자막이 없는 경우 다운로드 X)
'writethumbnail': 'best', # 영상 thumbnail 다운로드
'writeautomaticsub': True, # 자동 생성된 자막 다운로드
'subtitleslangs': 'en' # 자막 언어가 영어인 경우(다른 언어로 변경 가능)
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
except Exception as e:
print('error', e)
def download():
# short https://youtube.com/shorts/nca6easwPB4 ---> https://youtube.com/watch?v=nca6easwPB4
youtube_url_list = [ 'https://www.youtube.com/watch?v=tvsxt9Wjpp4&list=WL&index=2' ]
download_video_and_subtitle(VIDEO_DOWNLOAD_PATH, youtube_url_list)
def download2():
# short https://youtube.com/shorts/nca6easwPB4 ---> https://youtube.com/watch?v=nca6easwPB4
youtube_url_list = [ 'https://www.youtube.com/watch?v=tvsxt9Wjpp4&list=WL&index=2' ]
ydl_opts = {
'quiet': True,
'verbose': False,
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'outtmpl': f'{VIDEO_DOWNLOAD_PATH}%(title)s.%(ext)s',
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download(youtube_url_list)
def convert():
import moviepy.editor as mp
clip = mp.VideoFileClip(r"D:\videos\Always Remember Us This Way.mp4")
clip.audio.write_audiofile(r"D:\videos\Always Remember Us This Way.mp3")
def extract_frames():
import cv2
filename = r"C:\Users\MarketPoint\Downloads\탁구서브.mp4"
output_dir = r"D:\temp\frames"
vidcap = cv2.VideoCapture(filename)
count = 0
while(vidcap.isOpened()):
retval, image = vidcap.read()
if not retval:
break
# 이미지 사이즈 960x540으로 변경
#image = cv2.resize(image, (960, 540))
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
# 30프레임당 하나씩 이미지 추출
if(int(vidcap.get(1)) % 1 == 0):
print('Saved frame number : ' + str(int(vidcap.get(1))))
# 추출된 이미지가 저장되는 경로
cv2.imwrite(os.path.join(output_dir,f"frame{count:0>4}.png"), image)
#print('Saved frame%d.jpg' % count)
count += 1
vidcap.release()
if __name__ == '__main__':
download2()
#convert()
#extract_frames()
print('Complete download!')