YouTube¤
Note
In general, the YouTube API is sometimes oddly limited for users that are not a content network. Some workarounds are necessary.
Warning
The Google-API wrapper currently does not work for macOS Sequoia, yet (22.09.2024).
Preparations / Prerequisites¤
👉A Google user account is required.
👉This user account needs to have access to the YouTube channel.
👉A Google Cloud Project with YouTube API V3 enabled. See the required API access below..
👉Add the YouTube channel id to the local configuration file `config_local.yaml.
youtube:
channels:
my_channel:
id: "MYCHANNELID"
from pytube.handlers.youtube import YT
yt = YT()
yt.get_channel_id()
Find the YouTube Channel ID¤
Log into YoTube, switch to the channel and go to this url to see the channel id.
Uploading Videos¤
👉Videos need to be uploaded via the 🖥 YouTube studio web interface️.
By the way, API Uploads is not an option as they make videos 'private' by default,
and there is no way to change this setting afterward.
Uploading is easy: one can upload 15 videos at a time by drag and drop.
👉All following steps are required:
- Include Pretix-Session-ID in file names, e.g., {PreTix-Session-ID}-{Title}.mp4.
- Make sure in YouTube Studio in Settings/Upload defaults the title is empty.
YouTube will use the filename as title then, we need this to match the descriptions' metadata to the video uploads. - Add all videos uploaded to a hidden playlist (workaround)
YouTube Video IDs via a Hidden Playlist¤
After videos are uploaded to YouTube, we need to update the metadata.
To update the metadata, we need the YouTube video ids.
Metadata of unpublished videos (NOT 'private' or 'draft') is not available via the YouTube API but can be retrieved via an unpublished playlist (workaround).
Add the playlist id in the local_config.yaml file:
youtube:
channels:
my_channel:
playlist_id: "playlist_id_ADD_HERE"
Mapping YouTube Video IDs to Pretalx IDs¤
You need to prepare mapping files for the processing pipeline.
These files are expected by the handler (Publisher) by convention.
The mapping process now includes safety checks:
- Automatically skips videos marked as do_not_record
- Respects channel assignments from video organization
- Warns about videos that shouldn't be on YouTube
Using the CLI (recommended):
# Map videos with safety checks enabled
pytube youtube map
# Only map videos assigned to specific channel
pytube youtube map --filter-channel pydata
# Include do_not_record videos (NOT RECOMMENDED)
pytube youtube map --include-do-not-record
Using Python directly:
from pytube.handlers.youtube import YT
yt = YT()
yt.get_youtube_ids_for_uploads("my_channel")
# Match the pretalx id with the YouTube video id
# This now includes safety checks by default
mapping, warnings = yt.map_pretalx_id_youtube_id(
skip_do_not_record=True, # Skip do_not_record videos (default)
filter_by_channel="pydata" # Optional: only map videos for this channel
)
Caveats¤
👉 It's impossible to get the filename used for the upload via the API, although the information is
available via the web interface. Do follow the steps above, step 2.
👉 API limits: Queries are limited per day, use them wisely.
YouTube Descriptions¤
The descriptions are generated via a jinja2 template.
Please feel free to use the example in /templates as a starting point.
If you use your own template, and require additional attributes, you can customize the PrepareVideoMetadata class.
scripts/youtube.py provides an example of how to do this.
from pytube.handlers.youtube import PrepareVideoMetadata
class CustomPrepareVideoMetadata(PrepareVideoMetadata):
"""
Patch PrepareVideoMetadata to customize attributes used in the description template.
"""
@classmethod
def customize_description_args(cls, description_kwargs, record):
"""Customized the description arguments."""
description_kwargs.update({
"tag": "#great",
"pydata": record.youtube_channel == "pydata"
})
return description_kwargs
Scheduling Videos¤
The PrepareVideoMetadata handler class provides a method to set the publishing date of the videos in the video metadata.
from datetime import datetime, timedelta, UTC
from pytube.handlers.youtube import PrepareVideoMetadata
meta = PrepareVideoMetadata("template_file", "great conference name")
...
# when to start and how often to publish
in_five_minutes = datetime.now(UTC) + timedelta(minutes=5)
every_four_hours = timedelta(hours=4)
meta.update_publish_dates(start=in_five_minutes, delta=every_four_hours)
API¤
Access to the YouTube API V3 is required to update the metadata of the videos. To access the YouTube API V3 a Google Cloud project is required.
How to Get a YouTube API Key¤
- Log in to Google Developers Console.
- Create a new project.
- On the new project dashboard, click Explore & Enable APIs.
- In the library, navigate to YouTube Data API v3 under YouTube APIs.
- Enable the YouTube API V3.
- Create credentials:
a. API key
b. OAuth2.0 client ID c. target group external in test mode, add test users.
There are plenty of detailed tutorials available on the web.
Caveats¤
👉 Metadata updates require OAuth2.0 authentication. API keys or service accounts are not sufficient.
👉 Status updates can be requested with a simple API key.