批量通过命令行python将视频上传到youtube

时间:2016-05-01 02:01:17

标签: python batch-file video youtube

我正在使用youtube-upload中的脚本通过命令行将视频上传到我的频道。
如何运行代码来处理特定文件夹中的所有文件?
一旦我在网上看到一个例子,展示如何使用相同的脚本批量上传视频到youtube,以及从csv excel文件获取标题,类别,描述和标签,每个文件都来自与另一个文件名对应的列柱。我忘了保存这个例子,现在我似乎无法找到它或者自己创建它。

顺便说一句,我在Windows 10环境下,使用命令行工具。

上传单个视频,我正在使用此脚本;

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=client_secrets.json \
 --credentials-file=client_secrets.json \
 test.mp4

更新

csv文件将具有与youtube-upload命令中的参数相同的列数。比方说,只会有标题,描述,类别,标签列,当然第一列将是文件名,如果需要,我还会添加文件的位置。

2 个答案:

答案 0 :(得分:3)

:: == 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 :(得分:3)

使用python脚本,您将使用子进程模块来调用youtube-upload程序。您可以将此实用程序与csv模块和argparse组合使用。

该计划的核心是:

import csv
import subprocess

def upload(csvfile):
    with open(csvfile') as f:
        for info in csv.DictReader(f):
            info.update({'client-secrets':'client_secrets.json', 'credentials-file':'client_secrets.json')
            subprocess.call(['youtube-upload'] + ['--{0}="{1}"'.format(k,v) for k,v in info.items()]}) 

应从Excel导出CSV文件,以便引用逗号。我们在CSV中使用标题行来定义将在命令行上传递给youtube-upload的参数。

整个实用程序可能如下所示:

#!python
"""
Upload media files specified in a CSV file to YouTube using youtube-upload script.

CSV File exported from Excel (i.e. commas properly quoted)
First line contains upload parameters as column headers
Subsequent lines contain per media file values for the corresponding options.
e.g.
file,description,category,tags...
test.mp4,A.S. Mutter,A.S. Mutter plays Beethoven,Music,"mutter, beethoven"
etc...

"""
import csv
import subprocess

def upload(csvfile):
    with open(csvfile) as f:
        for info in csv.DictReader(f):
            info.update({'client-secrets':'client_secrets.json', 'credentials-file':'client_secrets.json'})
            commandline = ['youtube-upload'] + ['--{0}="{1}"'.format(k,v) for k,v in info.items()]
            #print commandline
            subprocess.call(commandline) 

def main():
    import argparse
    p = argparse.ArgumentParser(description='youtube upload the media files specified in a CSV file')
    p.add_argument('-f', '--csvfile', default='vids.csv', 
        help='file path of CSV file containing list of media to upload')
    args = p.parse_args()

    upload(args.csvfile)

if __name__ == '__main__':
    main()

进入Python后,您可能需要调查YouTube API which can be accessed directly from Python

开始时我会使用youtube-upload实际上可以加载为python模块的事实,然后调用subprocess而不是import youtube-upload youtube-upload.main(commandline)并调用{{1}} }。