Like many people, I access Internet via an ADSL connection at home. ADSL stands for “Asymmetric Digital Subscriber Line”, with Asymmetric being the key word here, as it just mean your download speed will be (much) higher than your upload speed. My ISP promises a theoretical 10 Mbps download speed, and 512 Kbps upload speed, and this is pretty close in reality: Data Rate: 10240 (downstream), 509 (upstream) kbps. Assuming a 265MB video, in the very best case (63 KB/s), it would take 1 hour and 12 minutes to upload a video to YouTube, but in practice it’s often closer to 2 or 3 hours.
If it’s a video you’ve shot yourself, and copied inside your computer, there’s very little you can do, except processing the video with tools such as HandBrake to make it smaller before upload. But if the video files are located somewhere in the Internet, and you happen to have access to a VPS or dedicated server, you could consider uploading the video from the command line using your server. A Youtube Upload script could also be useful to automatize video uploads from a CCTV camera for example.
A Google search quickly found a script to do just that. Yeess! The Python script youtube-upload can be downloaded and installed to upload videos to YouTube from the command line. Unfortunately, it does not seem to work anymore. My upload scenario is a little complex to handle as I’ve tried to login in a different country (the server is based in the US, whereas I usually login from Asia), and Google found this to be dodgy, sending me both an email and an SMS to tell me about a “Suspicious sign in prevented”. But even as I tried on my local machine, I got an HTTP 403 (Forbidden) reply.
Time for plan B. Google happens to have its own upload_video.py script, and I could make it work with some, or rather a lot of, efforts mainly because of authentication. There’s probably a better way (comments welcome), but here’s what I had to do in Ubuntu 12.04 / 13.10:
- Download the script:
123mkdir youtube-uploadcd youtube-uploadwget https://github.com/youtube/api-samples/raw/master/python/upload_video.py - Download and extract Google API Python Client, and move relevant directories to the directory where the script is located:
12345wget https://google-api-python-client.googlecode.com/files/google-api-python-client-1.2.tar.gztar xzvf google-api-python-client-1.2.tar.gzcd google-api-python-client-1.2/mv apiclient/ oauth2client/ uritemplate/ ..cd .. - In a computer with a Desktop interface, create a project called “Youtube Upload Script” with Google Cloud Console (the name and id does not matter) using the YouTube credential, and
- Back to the command line, create a file client_secrets.json vi your preferred editor (e.g. nano, vi, …), with the Client ID, Client secret, and redirect URIs above:
123456789{"installed": {"client_id": "xxxxxxxxxx-yyyyyyyyyyyyyyy.apps.googleusercontent.com","client_secret":"ABCDXXxxxxxxxxx-CddddddddD","redirect_uris": ["http://locahost", "urn:ietf:wg:oauth:2.0:oob"],"auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://accounts.google.com/o/oauth2/token"}} - Now you can upload a video to your YouTube account with the script. There are two cases:
- You have access to a desktop browser such as Chromium or Firefox on the machine where the script is executed:
123python upload_video.py --file=video.webm --title="Video Title" \--description="Video Description" --keywords="keyword1, keyword2" \--category=22 --privacyStatus="unlisted"
Most options are self-descriptive. Category 22 refers to People and Blog. other categories available are: Comedy “23”, Education “27”, Entertainment “24”, Film & Animation “1”, Gaming “20”, Howto & Style “26”, Music “10”, News & Politics “25”, Nonprofits & Activism “29”, People & Blogs “22”, Pets & Animals “15”, Science & Technology “28”, Sports “17”, and Travel & Events “19”.privacyStatus lets you choose between unlisted, private or public.
- The machine where the script is executed does not have access to a desktop browser, and you need to use a browser in another machine, run the following command instead:
123python upload_video.py --file=video.webm --title="Video Title" \--description="Video Description" --keywords="keyword1, keyword2" \--category=22 --privacyStatus="unlisted" <span style="color: #008000;">--noauth_local_webserver</span>
It will provide a link to access in your browser, to get a verification code that you can enter in the terminal. This is only requires for the first time, after you can just upload the videos without user intervention for authentication.
If you run the command without noauth_local_webserver, and if you only have access to a text based web browser this won’t work, as they don’t usually support JavaScript.
- You have access to a desktop browser such as Chromium or Firefox on the machine where the script is executed:
- That’s it, the video should now upload to your YouTube account.
123Authentication successful.Uploading file...Video id '64sT_voVhhY' was successfully uploaded.
I’ve tested it with one of FOSDEM 2014 videos, namely Grate Liberating the Tegra GPU with 265 MB of data, and instead of the 2 or 3 hours it would have taken on my home computer, it only took 31 seconds in a VPS…
One issue with this method is that client_secrets.json can not be distributed, as it contains your API credential, but if you want to create a script that easily usable by others, there are other methods which I haven’t tried.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.
Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress
Nice! Easy to follow instructions; worked flawlessly. Thank you for this–most appreciated.
I seem to be getting the following errors:
Uploading file…
Traceback (most recent call last):
File “upload_video.py”, line 171, in
initialize_upload(options)
File “upload_video.py”, line 112, in initialize_upload
resumable_upload(insert_request)
File “upload_video.py”, line 122, in resumable_upload
status, response = insert_request.next_chunk()
File “build/bdist.linux-x86_64/egg/oauth2client/util.py”, line 132, in positional_wrapper
File “build/bdist.linux-x86_64/egg/apiclient/http.py”, line 808, in next_chunk
apiclient.errors.ResumableUploadError:
@ZA
I’m not the developer of the script, so I’m not quite sure. But maybe it’s a login issue seeing it’s in “oauth2client”.
very nice but couldn’t you just email the video to youtube ?
Great job. Just an addon , the python script uses httplib2 which is not included in standard python installations, and you will have the following result : ===== Traceback (most recent call last): File “upload_video.py”, line 4, in import httplib2 ImportError: No module named httplib2 ===== you have to manually include it ; you can install it with pip i presume or in most cases, if you have no admin privileges on the target machine, you need to manually include it in your script maybe you have to change versions, in my case it was : ===== >$ wget https://pypi.python.org/packages/source/h/httplib2/httplib2-0.9.zip#md5=bdad6a844e0cb890b681fe7c811cfea5… Read more »
how to make the script work with multiple youtube accounts ?
Is it possible to turn monetization on?
take caution when setting up your credentials for OAuth. you must select OTHER for your client ID. I selected web and had a problem retrieving the verification code.
FYI this is what works for upload:
youtube-upload –client-secrets=client_secrets.json –title=”Salted Margaritas” –category=Party /Users/Movies/party/VID00163.MP4
Hi you!
I have multi chanel: examaple: chanel1, chanel2
When you using command is require “Enter verification code:”. I must fill code to command line. How to fix code for auto upload.