|
| 1 | +import argparse |
| 2 | + |
| 3 | +from moviepy.editor import * |
| 4 | +from pytube import YouTube |
| 5 | + |
| 6 | + |
| 7 | +def videoDownload(urls, path=None, hd=False): |
| 8 | + for url in urls: |
| 9 | + # Check to download the video in the highest resolution |
| 10 | + if hd: |
| 11 | + # Try,except block |
| 12 | + try: |
| 13 | + |
| 14 | + # Create the data stream in the highest resolution |
| 15 | + data = YouTube(url).streams.get_highest_resolution().download(output_path=path) |
| 16 | + |
| 17 | + # Get the name of the video |
| 18 | + filename = os.path.basename(data) |
| 19 | + |
| 20 | + print(f"File Downloaded: {filename}") |
| 21 | + print(f"File Path: {data}\n") |
| 22 | + |
| 23 | + # Get the error if any occurs |
| 24 | + except Exception as err: |
| 25 | + |
| 26 | + print(f"Error Occurred: {err}\n") |
| 27 | + |
| 28 | + else: |
| 29 | + # Try,except block |
| 30 | + try: |
| 31 | + |
| 32 | + # Create the data stream |
| 33 | + data = YouTube(url).streams.first().download(output_path=path) |
| 34 | + |
| 35 | + # Get the name of the video |
| 36 | + filename = os.path.basename(data) |
| 37 | + |
| 38 | + print(f"File Downloaded: {filename}") |
| 39 | + print(f"File Path: {data}\n") |
| 40 | + |
| 41 | + # Get the error if any occurs |
| 42 | + except Exception as err: |
| 43 | + |
| 44 | + print(f"Error Occurred: {err}\n") |
| 45 | + |
| 46 | + |
| 47 | +def audioDownload(urls, path=None, keep_video=False, hd=False): |
| 48 | + for url in urls: |
| 49 | + # Try,except block |
| 50 | + try: |
| 51 | + |
| 52 | + if hd: |
| 53 | + # Create the data stream |
| 54 | + data = YouTube(url).streams.first().download(output_path=path) |
| 55 | + |
| 56 | + else: |
| 57 | + |
| 58 | + # Create the data stream in the highest resolution |
| 59 | + data = YouTube(url).streams.get_highest_resolution().download(output_path=path) |
| 60 | + |
| 61 | + # Get the filename of the video |
| 62 | + filename, extension = (os.path.splitext(os.path.basename(data))) |
| 63 | + |
| 64 | + # Create the video object, then get the audio of the video |
| 65 | + video = VideoFileClip(data) |
| 66 | + audio = video.audio |
| 67 | + |
| 68 | + # Write the audio file, filename as the video name |
| 69 | + audio.write_audiofile(f"{filename}.mp3") |
| 70 | + |
| 71 | + # Change the location of the file, if path is provided |
| 72 | + if path: |
| 73 | + os.rename(f"{os.getcwd()}\{filename}.mp3", f"{path}\{filename}.mp3") |
| 74 | + |
| 75 | + # Close the video object |
| 76 | + video.close() |
| 77 | + |
| 78 | + # Check for to keep the downloaded video |
| 79 | + if keep_video: |
| 80 | + |
| 81 | + print(f"File Downloaded: {filename}.mp4") |
| 82 | + print(f"File Path: {data}\n") |
| 83 | + |
| 84 | + print(f"File Downloaded: {filename}.mp3") |
| 85 | + print(f"File Path: {(os.path.splitext(data))[0]}.mp3\n") |
| 86 | + |
| 87 | + else: |
| 88 | + # Check if file exists |
| 89 | + if os.path.isfile(data): |
| 90 | + # Remove the video file |
| 91 | + os.remove(data) |
| 92 | + |
| 93 | + print(f"File Downloaded: {filename}.mp3") |
| 94 | + print(f"File Path: {(os.path.splitext(data))[0]}.mp3\n") |
| 95 | + |
| 96 | + # Get the error if any occurs |
| 97 | + except Exception as err: |
| 98 | + |
| 99 | + print(f"Error Occurred: {err}") |
| 100 | + |
| 101 | + |
| 102 | +def main(): |
| 103 | + parser = argparse.ArgumentParser(description='DOWNLOAD AUDIO AND VIDEO FILES FROM YOUTUBE') |
| 104 | + |
| 105 | + # Argument is for a urls to be provided |
| 106 | + parser.add_argument('-u', dest='urls', nargs='+', type=str, help='URLS To Download From', required=True) |
| 107 | + |
| 108 | + # Argument is to download audios |
| 109 | + parser.add_argument('-a', dest='audio', action='store_true', help='Download Audio File') |
| 110 | + |
| 111 | + # Argument is to download videos |
| 112 | + parser.add_argument('-v', dest='video', action='store_true', help='Download Video File') |
| 113 | + |
| 114 | + # Argument is for filepath |
| 115 | + parser.add_argument('-p', dest='path', type=str, help='Path For To Store The Files') |
| 116 | + |
| 117 | + # Argument is used to keep the video with the audio file |
| 118 | + parser.add_argument('-k', dest='keepVideo', action='store_true', help='Keep The Video With The Audio File') |
| 119 | + |
| 120 | + # Argument is used to download video in highest resolution |
| 121 | + parser.add_argument('-hd', dest='hd', action='store_true', help='Download The HD Video') |
| 122 | + |
| 123 | + args = parser.parse_args() |
| 124 | + |
| 125 | + if args.urls: |
| 126 | + |
| 127 | + if args.video: |
| 128 | + |
| 129 | + if args.path and args.hd: |
| 130 | + videoDownload(args.urls, args.path, True) |
| 131 | + |
| 132 | + elif args.hd: |
| 133 | + videoDownload(args.urls, None, True) |
| 134 | + |
| 135 | + elif args.path: |
| 136 | + videoDownload(args.urls, args.path) |
| 137 | + |
| 138 | + else: |
| 139 | + videoDownload(args.urls) |
| 140 | + |
| 141 | + elif args.audio: |
| 142 | + |
| 143 | + if args.path and args.keepVideo and args.hd: |
| 144 | + audioDownload(args.urls, args.path, True, True) |
| 145 | + |
| 146 | + elif args.path and args.keepVideo: |
| 147 | + audioDownload(args.urls, args.path, True) |
| 148 | + |
| 149 | + elif args.keepVideo and args.hd: |
| 150 | + audioDownload(args.urls, None, True, True) |
| 151 | + |
| 152 | + elif args.keepVideo: |
| 153 | + audioDownload(args.urls, None, True) |
| 154 | + |
| 155 | + elif args.path: |
| 156 | + audioDownload(args.urls, args.path) |
| 157 | + |
| 158 | + else: |
| 159 | + audioDownload(args.urls) |
| 160 | + |
| 161 | + else: |
| 162 | + print("Use -v for Video Download and -a For Audio Download") |
| 163 | + print("Use -h for help") |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == '__main__': |
| 167 | + main() |
0 commit comments