如何使用Python上傳Youtube影片?

安裝依賴套件

pip install google-api-python-client oauth2client progressbar2
#Linux若無法安裝請加上sudo在最前面

安裝youtube-upload

git clone https://github.com/tokland/youtube-upload
cd youtube-upload
python setup.py install
#Linux若無法安裝請加上sudo在最前面

設定Google YouTube Data API

1.開啟Google Console
2.新增專案
3.啟用API與服務->YouTube Data API v3->啟用
4.左方憑證->建立憑證->OAuth用戶端ID->應用程式類型:其他名稱填寫:youtube-upload
5.下載回來的json檔案要存成".youtube-upload-credentials.json"的檔案名稱
6.詳細步驟參考下方圖片
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
下載回來的json檔案要存成”.youtube-upload-credentials.json”的檔案名稱

上傳影片

youtube-upload --title="VideoFromPython" VideoFromPython.mp4

錯誤:
module ‘oauth2client’ has no attribute ‘file’

將from oauth2client import file放入"youtube_upload/auth/__init__.py"中

詳細參數

  • Upload a video with extra metadata, with your own client secrets and credentials file, and to a playlist (if not found, it will be created):
$ youtube-upload \
--title="A.S. Mutter" " \
--description="A.S. Mutter plays Beethoven" \
--category="Music" \
--tags="mutter, beethoven" \
--recording-date="2011-03-10T15:32:17.0Z" \
--default-language="en" \
--default-audio-language="en" \
--client-secrets="my_client_secrets.json" \
--credentials-file="my_credentials.json" \
--playlist="My favorite music" \
--embeddable=True|False \
anne_sophie_mutter.flv

Other extra medata available :

--privacy (public | unlisted | private)  
--publish-at (YYYY-MM-DDThh:mm:ss.sZ)
--location (latitude=VAL,longitude=VAL[,altitude=VAL])
--thumbnail (string)
  • Upload a video using a browser GUI to authenticate:
$ youtube-upload --title="A.S. Mutter" --auth-browser anne_sophie_mutter.flv
  • Split a video with ffmpeg

If your video is too big or too long for Youtube limits, split it before uploading:

$ bash examples/split_video_for_youtube.sh video.avi
video.part1.avi
video.part2.avi
video.part3.avi
  • Use a HTTP proxy

Set environment variables http_proxy and https_proxy:

$ export http_proxy=http://user:password@host:port
$ export https_proxy=$http_proxy
$ youtube-upload ....

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store