-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconversion.py
218 lines (188 loc) · 8.49 KB
/
conversion.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
File for conversion logic
"""
from global_variables import *
from pytube import YouTube, Playlist
import requests
from bs4 import BeautifulSoup
from moviepy.editor import AudioFileClip
import os
import re
import csv
import datetime
"""
Move Conversion class here
-- COULD NOT MOVE IT DUE TO A STRANGE MOVIEPY ERROR THAT COULD NOT BE FIXED --
"""
class Conversion:
"""
This python class contains code for the PyTube conversion logic which allows
the program to read the data from the UI elements like the text entry,
stream quality, and radio buttons to convert to a mp3 or mp4 from a YouTube
video accordingly.
"""
def is_playlist(self, url):
"""Check if the URL corresponds to a YouTube playlist."""
return 'list=' in url
def convert_video(self, yt_url):
"""
Converts to video mp4.
:param yt_url:
:return:
"""
if self.is_playlist(yt_url):
self.video_playlist(yt_url)
else:
# The current date in format Day/Month/Year Hour:Minute:Seconds
date = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
# get YT URL and display progress bar for download
video = YouTube(yt_url, use_oauth=True, allow_oauth_cache=True,
on_progress_callback=self.download_progress)
# File size in MB
file_size = 0
# get video title
video_title = self.fetch_yt_video_title(yt_url)
print(f"Converting: {video_title} @{yt_url}")
# Make the video title a legal name for file saving
fixed_title = re.sub(r'[<>:"/\\|?*]', '', video_title)
# convert based on specific stream quality
if stream_quality == "Max":
stream = video.streams.get_highest_resolution()
file_size = stream.filesize_mb # get file size in mb
stream.download(output_path=file_save_location,
filename=f"{fixed_title}.mp4")
elif stream_quality == "Medium":
stream = video.streams.filter(res="720p",
file_extension='mp4').first()
file_size = stream.filesize_mb # get file size in mb
stream.download(output_path=file_save_location,
filename=f"{fixed_title}.mp4")
elif stream_quality == "Low":
stream = video.streams.filter(res="480p",
file_extension='mp4').first()
file_size = stream.filesize_mb # get file size in mb
stream.download(output_path=file_save_location,
filename=f"{fixed_title}.mp4")
else:
print(f"You Did Not Select A Stream Quality!")
# Save the current conversion in the conversion_history.csv file
with open(conversion_records, 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(['MP4', video_title, file_size, date])
print("YouTube Video Converted to mp4 successfully!")
def convert_audio(self, yt_url):
"""
Converts to audio mp3.
:param yt_url:
:return:
"""
if self.is_playlist(yt_url):
self.audio_playlist(yt_url)
else:
audio_file = ''
# File size in MB
file_size = 0
# The current date in format Day/Month/Year Hour:Minute:Seconds
date = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
# get YT URL and display progress bar for download
video = YouTube(yt_url, use_oauth=True, allow_oauth_cache=True,
on_progress_callback=self.download_progress)
# get video title
video_title = self.fetch_yt_video_title(yt_url)
print(f"Converting: {video_title} @{yt_url}")
# Make the video title a legal name for file saving
fixed_title = re.sub(r'[<>:"/\\|?*]', '', video_title)
# convert based on specific stream quality
if stream_quality == "Max":
stream = video.streams.filter(only_audio=True).order_by(
'abr').last()
file_size = stream.filesize_mb # get file size in mb
audio_file = stream.download(output_path=file_save_location)
elif stream_quality == "Medium":
stream = video.streams.filter(only_audio=True).order_by(
'abr').first()
file_size = stream.filesize_mb # get file size in mb
audio_file = stream.download(output_path=file_save_location)
elif stream_quality == "Low":
stream = video.streams.filter(only_audio=True).order_by(
'abr').first()
file_size = stream.filesize_mb # get file size in mb
audio_file = stream.download(output_path=file_save_location)
else:
print(f"You Did Not Select A Stream Quality!")
# Convert the PyTube WebM audio file to MP3
audio = AudioFileClip(audio_file)
audio.write_audiofile(f"{file_save_location}/{fixed_title}.mp3")
os.remove(audio_file) # remove original WebM file created by PyTube
audio.close()
# Save the current conversion in the conversion_history.csv file
with open(conversion_records, 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(['MP3', video_title, file_size, date])
print("YouTube Video converted to mp3 successfully!")
def audio_playlist(self, playlist_url):
"""
Logic for converting a playlist for audio mp3.
:return:
"""
current_video = 0 # track count of current video
playlist = Playlist(playlist_url)
print(f"\nStarted Converting Playlist: {playlist.title} @ "
f"{datetime.now().strftime('%d/%m/%Y %H:%M:%S')}!\n")
# Iterate through each video URL in playlist,
# converting the video to mp3
for video_url in playlist.video_urls:
self.convert_audio(video_url)
current_video += 1
# Provide progress update during conversion
print(f"\n{current_video} Processed Out Of {len(playlist)} Videos!"
f"\n")
# Indicate playlist conversion is complete
print(f"\nFinished Converting Playlist: {playlist.title} @ "
f"{datetime.now().strftime('%d/%m/%Y %H:%M:%S')}!\n")
def video_playlist(self, playlist_url):
"""
Logic for converting a playlist for video mp4.
:return:
"""
current_video = 0 # track count of current video
playlist = Playlist(playlist_url)
print(f"\nStarted Converting Playlist: {playlist.title} @ "
f"{datetime.now().strftime('%d/%m/%Y %H:%M:%S')}!\n")
# Iterate through each video URL in playlist,
# converting the video to mp4
for video_url in playlist.video_urls:
self.convert_video(video_url)
current_video += 1
# Provide progress update during conversion
print(f"\n{current_video} Processed Out Of {len(playlist)} Videos!"
f"\n")
# Indicate playlist conversion is complete
print(f"\nFinished Converting Playlist: {playlist.title} @ "
f"{datetime.now().strftime('%d/%m/%Y %H:%M:%S')}!\n")
def fetch_yt_video_title(self, url):
"""
This function fetches the title of the YouTube video via URL.
:return: str
"""
# Make request to get url
response = requests.get(url)
# Transform HTML text into structured format
soup = BeautifulSoup(response.text, 'html.parser')
# locate and set title,
# removing the last 10 characters
title = soup.find('title').string
return title[:-10]
def download_progress(self, stream, chunk, bytes_remaining):
"""
This function displays a percent to show how much of the
download has progressed.
:return:
"""
# Get total file size
total_size = stream.filesize
# Calculate bytes downloaded
bytes_downloaded = total_size - bytes_remaining
# Calculate percentage downloaded and display
percentage_downloaded = bytes_downloaded / total_size * 100
print(f"{percentage_downloaded:.0f}% Downloaded!")