Skip to content

Commit 381eab1

Browse files
committed
Youtube Downloader Added
1 parent a688770 commit 381eab1

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

Youtube Downloader/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# YouTube Download Script
2+
The python script can download both video and audio files from YouTube
3+
4+
What the program does?
5+
- Download video,audio files from YouTube
6+
- Accepts multiple URLs at the same time
7+
- Can specify where to store the files
8+
- Can download videos in there the highest resolution available
9+
- Saves audio files in .mp3 format
10+
11+
### Requirements
12+
Python3
13+
> pip install -r requirements.txt
14+
15+
### Usage
16+
```
17+
ytavdownload.py [-h] -u URLS [URLS ...] [-a] [-v] [-p PATH] [-k] [-hd]
18+
19+
DOWNLOAD AUDIO AND VIDEO FILES FROM YOUTUBE
20+
21+
optional arguments:
22+
-h, --help show this help message and exit
23+
-u URLS [URLS ...] URLS To Download From
24+
-a Download Audio File
25+
-v Download Video File
26+
-p PATH Path For To Store The Files
27+
-k Keep The Video With The Audio File
28+
-hd Download The HD Video
29+
30+
```
31+
32+
### Contribution
33+
34+
Any kind of contributions are welcome.
35+
36+
1. Fork the Project
37+
2. Commit your Changes
38+
3. Open a Pull Request
39+
40+

Youtube Downloader/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
moviepy==1.0.3
2+
pytube==10.4.1

Youtube Downloader/ytavdownload.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)