You can use GitHub Actions to automate the process of downloading and uploading a video to YouTube. Below are the steps to set up a GitHub Actions workflow for this purpose.
Upload Remote Videos to YouTube using GitHub Actions
YouTube Remote Uploader
YouTube Remote Uploader is a tool that automates the process of downloading videos from remote URLs and uploading them to YouTube using the YouTube Data API. This tool is particularly useful for batch processing and scheduled uploads using GitHub Actions.
Features
π Download videos from remote URLs
πΉ Upload videos to YouTube with metadata (title, description, tags, etc.)
π¦ Batch processing of multiple videos
π Automation with GitHub Actions
Getting Started
Prerequisites
π Python 3.x
π¦ Google API Client Library
π GitHub repository with necessary permissions and secrets
Installation
Clone the repository:
git clone https://github.com/SH20RAJ/youtube-remote-uploader.git
cd youtube-remote-uploader
Add the Python script (upload_video.py) to your GitHub repository. Ensure your credentials.json file and any other necessary files are added to the repository or securely stored.
Set Up GitHub Secrets:
Go to your GitHub repository, navigate to Settings > Secrets and variables > Actions, and add the following secrets:
GOOGLE_CREDENTIALS: The content of your credentials.json file.
Create a GitHub Actions Workflow:
Create a .github/workflows/upload_video.yml file in your repository with the following content:
name:Upload Video to YouTubeon:push:branches:-mainworkflow_dispatch:jobs:upload_video:runs-on:ubuntu-lateststeps:-name:Checkout repositoryuses:actions/checkout@v3-name:Set up Pythonuses:actions/setup-python@v4with:python-version:'3.x'-name:Install dependenciesrun:|python -m pip install --upgrade pippip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client requests-name:Set up Google Credentialsrun:echo "${{ secrets.GOOGLE_CREDENTIALS }}" > credentials.json-name:Run upload scriptrun:python upload_video.py
Modify Your Python Script:
Ensure your Python script (upload_video.py) reads the credentials from the credentials.json file created by the GitHub Action.
importosimportrequestsimportgoogle.authimportgoogle.auth.transport.requestsfromgoogle.oauth2.credentialsimportCredentialsfromgoogle_auth_oauthlib.flowimportInstalledAppFlowfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.httpimportMediaFileUpload# Set up OAuth 2.0 authorization
SCOPES=["https://www.googleapis.com/auth/youtube.upload"]defget_authenticated_service():creds=Noneifos.path.exists("token.json"):creds=Credentials.from_authorized_user_file("token.json",SCOPES)ifnotcredsornotcreds.valid:ifcredsandcreds.expiredandcreds.refresh_token:creds.refresh(google.auth.transport.requests.Request())else:flow=InstalledAppFlow.from_client_secrets_file("credentials.json",SCOPES)creds=flow.run_local_server(port=0)withopen("token.json","w")astoken:token.write(creds.to_json())returnbuild("youtube","v3",credentials=creds)defdownload_video(video_url,output_path):response=requests.get(video_url,stream=True)withopen(output_path,'wb')asfile:forchunkinresponse.iter_content(chunk_size=8192):file.write(chunk)defupload_video(service,video_file,title,description,tags,category_id,privacy_status):body={"snippet":{"title":title,"description":description,"tags":tags,"categoryId":category_id},"status":{"privacyStatus":privacy_status}}media=MediaFileUpload(video_file,chunksize=-1,resumable=True)request=service.videos().insert(part="snippet,status",body=body,media_body=media)response=request.execute()print(f"Video uploaded: {response['id']}")if__name__=="__main__":service=get_authenticated_service()# Remote video URL
video_url="https://example.com/path_to_your_video_file.mp4"# Temporary local path to save the video
local_video_file="temp_video.mp4"# Download video from remote URL
download_video(video_url,local_video_file)# Video details
title="Your Video Title"description="Your video description"tags=["tag1","tag2","tag3"]category_id="22"# See YouTube API documentation for category IDs
privacy_status="public"# "public", "private", or "unlisted"
# Upload video to YouTube
upload_video(service,local_video_file,title,description,tags,category_id,privacy_status)# Optionally, remove the temporary video file
os.remove(local_video_file)
Run the Workflow:
Push your changes to the main branch or manually trigger the workflow using the Actions tab in your GitHub repository.
This setup will allow you to automate the process of downloading a video from a remote URL and uploading it to YouTube whenever you push to the main branch or manually trigger the workflow.