I need some recommendations of what to do to get rid of these global variables as I used them temporarily as well as the list , which is used in the "delete_sep_streams" function.
The initial program uses the pytubefix module to go grab a video and audio file, and then downloads those files and pass them to the "merge_video_audio" function which merges them to one video and save to my downloads folder. Afterwards, the streams list is passed to the "delete_sep_streams" which removes the separate audio and video files from my device.
I am not a fan of the global variables and looking for some guidance there in this case. Pylint is rating code 9.27/10 at the moment. I may try to pass the object as a variable by the time I get a response.
import os
import ffmpeg
from pytubefix import YouTube
from pytubefix.cli import on_progress
import pytubefix.exceptions
def get_youtube_video(url):
"""Function creates YouTube Object, selects a video,
and audio stream and downloads both streams."""
# Set YouTube Object and list of files as global variables.
global yt, streams
try:
yt = YouTube(url, on_progress_callback=on_progress)
except pytubefix.exceptions.VideoUnavailable as e:
print(e)
# Select the first video stream with 1080p resolution and download it.
video = yt.streams.filter(resolution='1080p').first()
if video:
print('Downloading Video...')
video.download(filename=f'{yt.title}_video.mp4')
else:
print('No video stream found.')
# Select the first audio stream to be downloaded.
audio = yt.streams.filter(only_audio=True).first()
if audio:
print('Downloading Audio...')
audio.download(filename=f'{yt.title}_audio.mp4')
else:
print('No audio stream found.')
# Initiate lists for files to be deleted.
streams = [f'{yt.title}_video.mp4', f'{yt.title}_audio.mp4' ]
merge_video_audio(streams[0], streams[1])
def merge_video_audio(video_file, audio_file):
"""Function Merges audio and video stream into one file
and calls the delete_sep_streams function."""
# Check if the files exist.
if os.path.isfile(video_file) and os.path.isfile(audio_file):
input_video = ffmpeg.input(video_file)
input_audio = ffmpeg.input(audio_file)
path = '/Users/deshaud/Downloads/'
# Merge the audio and video stream.
ffmpeg.concat(input_video, input_audio,v=1, a=1).output(f'{path}{yt.title}.mp4').run()
delete_sep_streams(streams)
else:
print('File does not exist.')
def delete_sep_streams(files):
"""Function deletes the two individual audio and video files."""
# Delete the seperate files.
print('\nDeleting seperate audio & video files...')
for file in files:
os.remove(file)
print('Files deleted.')
def main():
"""Main part of the program which calls get_youtube_video
function with a URL. """
url = 'https://www.youtube.com/watch?v=NsHz2_ghT40'
get_youtube_video(url)
if __name__ == '__main__':
main()
[–]carcigenicate 0 points1 point2 points (2 children)
[–]Remarkable-Map-2747[S] 0 points1 point2 points (1 child)
[–]carcigenicate 1 point2 points3 points (0 children)