Last active
October 21, 2022 18:36
-
-
Save MorrisJobke/11235251 to your computer and use it in GitHub Desktop.
Youtube Playlist Downloader
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Youtube Playlist Downloader | |
# Copyright (C) 2014 Morris Jobke <[email protected]> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
import urllib.request, json, pprint, string, sys, subprocess, argparse, time, glob | |
# http://stackoverflow.com/a/17303428 | |
class color: | |
PURPLE = '\033[95m' | |
CYAN = '\033[96m' | |
DARKCYAN = '\033[36m' | |
BLUE = '\033[94m' | |
GREEN = '\033[92m' | |
YELLOW = '\033[93m' | |
RED = '\033[91m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
END = '\033[0m' | |
parser = argparse.ArgumentParser(description='Download all videos of a YouTube playlist') | |
parser.add_argument('apiKey', help='YouTube API key') | |
parser.add_argument('playlistId', help='playlist ID') | |
parser.add_argument('--maxResults', dest='maxResults', help='max results per API request (1..50)', default=50, type=int) | |
args = parser.parse_args() | |
url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&key=' + args.apiKey + '&playlistId=' + args.playlistId + '&maxResults=' + str(args.maxResults) | |
tmpUrl = url | |
videos = [] | |
videoFiles = glob.glob('*.mp4') | |
start = time.perf_counter() | |
while True: | |
data = json.loads(urllib.request.urlopen(tmpUrl).read().decode('utf8')) | |
for item in data['items']: | |
videoId = item['snippet']['resourceId']['videoId'] | |
videoTitle = item['snippet']['title'] | |
if len([s for s in videoFiles if s.endswith(videoId + '.mp4')]) != 0: | |
# don't process already avaiable files | |
print((color.BLUE + 'Skip {0}' + color.END).format(videoTitle)) | |
continue | |
videos.append({ | |
'id': videoId, | |
'url': 'http://youtu.be/' + item['snippet']['resourceId']['videoId'], | |
'description': item['snippet']['description'], | |
'title': videoTitle | |
}) | |
if 'nextPageToken' in data: | |
tmpUrl = url + '&pageToken=' + data['nextPageToken'] | |
else: | |
break | |
elapsed = (time.perf_counter() - start) | |
message = 'Elapsed time (YouTube API): ' + color.BOLD + color.GREEN + ' {:.1f} seconds' + color.END | |
print(message.format(elapsed)) | |
start = time.perf_counter() | |
count = 1 | |
videoCount = len(videos) | |
failed = [] | |
for video in videos: | |
message = 'Downloading ' + color.BOLD + '{0}' + color.END + '/{1} - ' + color.BOLD + '{2}' + color.END | |
print(message.format(count, videoCount, video['title'])) | |
pipe = subprocess.Popen(['youtube-dl', video['url']], stderr=subprocess.PIPE) | |
out, err = pipe.communicate() | |
video['error'] = str(err) | |
if pipe.returncode != 0: | |
print((color.BOLD + color.RED + 'FAILED' + color.END + ' {0}').format(str(err))) | |
failed.append(video) | |
count += 1 | |
elapsed = (time.perf_counter() - start) | |
message = 'Elapsed time (Download): ' + color.BOLD + color.GREEN + ' {:.1f} seconds' + color.END | |
print(message.format(elapsed)) | |
f = open('failed-videos.json', 'w') | |
json.dump(failed, f) | |
f.close() |
Gerade nochmal geupdated. Nun überspringt es bereits vorhandene Dateien.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So gerade nochmal ein Update gemacht.
Kann ich gerne machen.