Skip to content

Instantly share code, notes, and snippets.

@vonthecreator
Last active November 7, 2021 13:17
Show Gist options
  • Save vonthecreator/ea34733ecae0983d9307f3e5cb1b60bd to your computer and use it in GitHub Desktop.
Save vonthecreator/ea34733ecae0983d9307f3e5cb1b60bd to your computer and use it in GitHub Desktop.
youtube_dl learns how to upload to S3 buckets
import youtube_dl
import sys, os, time
import boto3
# VERSIONS:
# 1. boto3==1.18.63
# 2. youtube_dl==2021.6.6
#
# How to use:
# `python3 main.py "https://www.youtube.com/watch?v=LgmL4TQCNtI"`
downloads_dir = '/Users/<username>/Downloads/'
s3space = os.environ.get('S3_SPACE')
def client():
return boto3.session.Session().client('s3',
region_name='ewr1',
endpoint_url=os.environ.get("S3_HOST_URL"),
aws_access_key_id=os.environ.get("S3_KEY_ID"),
aws_secret_access_key=os.environ.get("S3_SECRET_KEY"))
# filename as filepath descriptor
def upload_file(filename):
try:
client().upload_file(filename, s3space, os.path.basename(filename))
return client().generate_presigned_url('get_object',
Params={
'Bucket': s3space,
'Key': os.path.basename(filename)
},
ExpiresIn=3600)
except BaseException as error:
print(error)
def youtubedl(link):
ydl_opts = {
'format': 'mp4',
'outtmpl': downloads_dir +"%(id)s.%(ext)s",
'no_check_certificate': True
}
try:
_id = link.strip()
meta = youtube_dl.YoutubeDL(ydl_opts).extract_info(_id)
return downloads_dir + meta['id'] + ".mp4"
except BaseException as e:
print(e)
return None
try:
print(upload_file(youtubedl(sys.argv[1])))
except BaseException as e:
print(e)
@vonthecreator
Copy link
Author

vonthecreator commented Nov 7, 2021

The code at the top does two things: Download youtube videos and upload to s3bucket.

Results

NOTE: For s3 bucket to work, you have to use your own ENVIRONMENT variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment