Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch upload videos to youtube via command line python

I am using the script from youtube-upload to upload videos to my channel via the command line.
How can I run the code to process all files in a specific folder(s)?
Once I saw an example on the net, showing how to batch upload videos to youtube using this same script, and also getting the titles, categories, description and tags from a csv excel file, each from a column corresponding to the file name in another column. I forgot to save this example and now I can't seem to find this or create it on my own.

by the way, I am on windows 10 environment, using the command line tool.

to upload single video, I am using this script;

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

update

the csv file will have the same number of columns as I will have parameters in the youtube-upload command. Let's say, there will be title, description, category, tags columns only, and of course the first column will be the file name, if needed, I will also add the location of the file.

like image 719
cplus Avatar asked May 01 '16 02:05

cplus


2 Answers

Using a python script you would use the subprocess module to call the youtube-upload program. You could write a little utility using this in combination with the csv module and perhaps argparse.

The core of the program is:

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()]}) 

The CSV file should be exported from Excel so that commas are quoted. We are using a header row in the CSV to define the parameters that will be passed on the command line to youtube-upload.

The whole utility could look like this:

#!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()

Once you are into Python you might like to investigate the YouTube API which can be accessed directly from Python.

To get started I'd use the fact that youtube-upload could in fact be loaded as a python module, then instead of calling subprocess you could import youtube-upload and call youtube-upload.main(commandline).

like image 75
Mike Robins Avatar answered Oct 21 '22 12:10

Mike Robins


:: == 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
like image 24
SomethingDark Avatar answered Oct 21 '22 12:10

SomethingDark