Skip to content

Instantly share code, notes, and snippets.

@MorrisJobke
Last active October 21, 2022 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MorrisJobke/11235251 to your computer and use it in GitHub Desktop.
Save MorrisJobke/11235251 to your computer and use it in GitHub Desktop.
Youtube Playlist Downloader
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Youtube Playlist Downloader
# Copyright (C) 2014 Morris Jobke <morris.jobke@gmail.com>
#
# 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()
@MorrisJobke
Copy link
Author

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