-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube3.py
More file actions
48 lines (33 loc) · 1.49 KB
/
youtube3.py
File metadata and controls
48 lines (33 loc) · 1.49 KB
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
import os
import subprocess
import argparse
def download_video(video_url, output_dir):
"""Download a single video with merged audio."""
print(f"\n🔹 Downloading: {video_url}")
os.makedirs(output_dir, exist_ok=True)
# Define the output file format
output_path = os.path.join(output_dir, "%(title)s.mp4")
# Download best video+audio format in one go
subprocess.run([
"yt-dlp", "-f", "bv+ba", "-o", output_path, video_url
], check=True)
print(f"✅ Download complete! Saved to: {output_dir}")
def download_playlist(playlist_url, output_dir):
"""Download all videos in a playlist with merged audio."""
print(f"\n📂 Downloading playlist: {playlist_url}")
os.makedirs(output_dir, exist_ok=True)
# Download all videos in playlist
subprocess.run([
"yt-dlp", "-f", "bv+ba", "-o", os.path.join(output_dir, "%(title)s.mp4"),
"--yes-playlist", playlist_url
], check=True)
print(f"✅ Playlist downloaded successfully in: {output_dir}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download YouTube videos or playlists with merged audio.")
parser.add_argument("url", type=str, help="YouTube video or playlist URL")
parser.add_argument("--output_dir", type=str, default="E:/download_video", help="Output directory")
args = parser.parse_args()
if "playlist?list=" in args.url:
download_playlist(args.url, args.output_dir)
else:
download_video(args.url, args.output_dir)