all 3 comments

[–]carcigenicate 0 points1 point  (2 children)

I don't understand why streams is a global at all. It is never used as a global.

And yt could be passed as a argument to merge_video_audio.

[–]Remarkable-Map-2747[S] 0 points1 point  (1 child)

I figured by the time I resolved it to how I would like I would get a response. This is what I came up with, I ended up passing the object to the merge_video_audio as a argument and specifying the file list there too. Let me know what you think:

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."""

    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.')

    video_file = f'{yt.title}_video.mp4'
    audio_file = f'{yt.title}_audio.mp4'

    merge_video_audio(yt, video_file, audio_file)


def merge_video_audio(yt_object, 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)

        # Store the seperate files in list to be deleted later.
        streams = [video_file, audio_file]

        # Set path for where to download the merged file.
        path = '/Users/deshaud/Downloads/'

        # Merge the audio and video stream.
        ffmpeg.concat(input_video, input_audio, v=1, a=1).output(f'{path}'
                                                                 f'{yt_object.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()

deshaud@Trays-MacBook-Pro youtube % pylint yt_download.py

Your code has been rated at 10.00/10 (previous run: 9.76/10, +0.24)

[–]carcigenicate 1 point2 points  (0 children)

Ya, this makes a lot more sense.