最近弄 Youtube 频道,感觉很累,一致手动上传,修改标题,修改 Description。然后就开始在网上找,看看有没有什么自动化工具。没想到还真让我看到了希望。在 Github 找到个 Youtube-upload 的项目。基于 Python 写出来的 Youtube API 项目。根据官方文档,看了下使用说明,看起来还挺简单的。不过你需要有 Python 环境先。或者在 Linux 或 MacOS 环境。

PS:提醒一下,批量上传因为 Youtube API 限制,只能上传5个视频左右,具体原因自己看底部。否则放弃吧。

pip install google-api-python-client oauth2client progressbar2

二、安装 youtube-upload

https://github.com/tokland/youtube-upload/archive/master.zip

下载上边的 Github 文件,解压缩zip文件,进入文件夹,使用 setup 安装

python setup.py install

三、获取 Google API

感觉这趴才是最复杂的一趴,Google 提供的 API 实在是太多了,之前创建过地图的API,所以这次创建Youtube API还是比较容易上手的。

1. 新建项目

2. 填写项目名称

3. 启用API

4. 启用 youtube API

5. 启用 Youtube Data API v3

5. 创建凭据

6. 下载 json 文件,重命名为

.client_secrets.json

然后将这个 json 文件放到你的用户名路径下,如:C:\Users\Forece\.client_secrets.json

四、使用 Linux 命令上传视频

如果没有安装一个 Git 就可以了。模拟了一下 Linux 环境。另外 Youtube-upload 项目也为了照顾 Windows 用户,里边还有个 bat 文件,直接运行 bat 文件也是可以滴。

简单上传命令:

youtube-upload --title="A.S. Mutter" anne_sophie_mutter.flv

带参数上传视频:

$ 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

上传遇到的错误还是挺多的,这里简单说一下把,首先就是之前的那个json文件,一定要放到指定文件夹里,另外注意文件名前边有个点。如果你不知道路径是什么,那么你直接简单运行一下代码,就会给你错误提示,然后告知你路径是什么了。

另外有大佬做了个脚本,可以批量上传

:: == ASSUMPTIONS ==
:: - this script is in the same directory as your CSV file
:: - your CSV lines are in the following order:
::   file_name;title;description;category;tags;recording_date
:: - Your descriptions do not contain semicolons
@echo off

set video_folder="C:\path\to\your\video\folder"

:: If your videos and csv file are in the same directory, you don't need the pushd or popd
:: Also, I couldn't get line continuation to work inside of the for loop, so everything
:: MUST be all on the same line.
pushd %video_folder%
for /f "tokens=1-6 delims=;" %%A in (vids.csv) do (
    youtube-upload --title="%%~B" --description="%%~C" --category="%%~D" --tags="%%~E" --recording-date="%%~F" --default-language="en" --default-audio-language="en" --client-secrets=client-secrets.json --credentials-file=client_secrets.json "%%~A"
)
popd
pause

最后总结一下:
问题1:实验了一下这个批量上传,发现根据 Youtube API 规则,被Google限制了10,000 Units。而上传一个视频会用掉1600 Units。结果就是每天只能上传5个视频。虽然有Form可以申请提高限额,但是太麻烦了。挂在服务器上,每天让他自己上传好了。上传一个就从列表自动减少一个。

问题2:
关于 Bat 命令,对中文支持不怎么好。可以在 CSV 中写入中文,但是不要在bat命令中写中文。