Last active
September 18, 2024 19:44
-
-
Save firminunderscore/ae49ebc08699104fdc1c17c7db6c46bc to your computer and use it in GitHub Desktop.
Download all videos of a youtube channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import googleapiclient.discovery | |
API_KEY = "YOUR_OWN_API_KEY" | |
def get_channel_id_for_channel_name(channel_name): | |
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey=API_KEY) | |
request = youtube.search().list( | |
part="snippet", | |
q=channel_name, | |
type="channel" | |
) | |
response = request.execute() | |
if "items" in response and len(response["items"]) > 0: | |
return response["items"][0]["snippet"]["channelId"] | |
else: | |
print("Channel not found.") | |
return None | |
def main(): | |
channel_name = input("Enter the YouTube channel name: ") | |
channel_id = get_channel_id_for_channel_name(channel_name) | |
if channel_id: | |
print(f"The channel ID is: {channel_id}") | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import googleapiclient.discovery | |
import googleapiclient.errors | |
import subprocess | |
API_KEY = "YOUR_OWN_API_KEY" | |
YOUTUBE_API_SERVICE_NAME = "youtube" | |
YOUTUBE_API_VERSION = "v3" | |
def get_channel_videos(channel_id): | |
youtube = googleapiclient.discovery.build( | |
YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=API_KEY) | |
video_ids = [] | |
next_page_token = None | |
while True: | |
request = youtube.search().list( | |
part="id", | |
channelId=channel_id, | |
maxResults=50, | |
pageToken=next_page_token, | |
order="date", | |
type="video" | |
) | |
response = request.execute() | |
for item in response.get("items", []): | |
video_ids.append(item["id"]["videoId"]) | |
next_page_token = response.get("nextPageToken") | |
if not next_page_token: | |
break | |
return video_ids | |
def download_videos(video_ids, start_index=None, end_index=None): | |
if start_index is not None and end_index is not None: | |
video_ids = video_ids[start_index:end_index+1] | |
for video_id in video_ids: | |
video_url = f"https://www.youtube.com/watch?v={video_id}" | |
cmd = f"yt-dlp {video_url}" | |
subprocess.run(cmd, shell=True) | |
def main(): | |
channel_id = input("Enter the YouTube channel ID (not the URL, but the ID, like UCxx123456): ") | |
video_ids = get_channel_videos(channel_id) | |
print(f"{len(video_ids)} videos found on this channel.") | |
choice = input("Do you want to download (1) all videos or (2) a specific range of videos? (Enter 1 or 2): ") | |
if choice == "2": | |
start_index = int(input("Enter the start index (0-based index): ")) | |
end_index = int(input("Enter the end index (0-based index): ")) | |
download_videos(video_ids, start_index, end_index) | |
else: | |
download_videos(video_ids) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment