-
-
Save MorrisJobke/11235251 to your computer and use it in GitHub Desktop.
#!/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() |
Super Arbeit danke! Ist deine Version denn "zuverlässig" ? Youtube-dl kann ja glaube ich auch playlisten runterladen bzw gab es dazu ein extra paket im AUR. Allerdings lädt das script einige videos nicht. Ich hoffe deins ist zuverlässiger mal ausprobieren :)
@shibumi: Hab gerade nochmal ausgebessert, da es bei fehlendem Video abbricht. Nun lädt es aber alle vorhanden Videos runter und schreibt alle nicht vorhanden (mit ID, Titel und Beschreibung) in failed-videos.json
. Dann kann man sich eventuell nochmal auf die Suche nach solch einem Video machen.
Nette Sache mal überlegt dazu ne PKGBUILD zu bauen und ins AUR hochzuladen? weil wie gesagt die "playlist-downloader" die ich kenne laden nie die komplette liste beim ersten versuch. Da muss man dann 2-3 mal neu anfangen eh man alle videos hat
So gerade nochmal ein Update gemacht.
Kann ich gerne machen.
Gerade nochmal geupdated. Nun überspringt es bereits vorhandene Dateien.
Needs youtube-dl (ArchLinux package) inside PATH and is tested with Python 3 on Arch Linux.
Youtube API key: https://developers.google.com/youtube/v3/