Command line YouTube upload with progress bar
The first technical post.
I upload videos to the channel using an adapted version of upload_video.py
from YouTube’s API code samples. The script does not provide a progress bar. This is usually not a problem because my connection is pretty fast, and when I upload from my Mac I can always bring up Activity Monitor to check bytes uploaded by the python process. However, when I upload from a shared remote server, sometimes due to spikes in load not under my control, upload would seem slower than usual; so I do want to see a progress bar occasonally.
upload_video.py
uses MediaFileUpload
which is a blackbox taking only the file path, and when you call next_chunk
on an insert request, technically a MediaUploadProgress
is returned which tells you the percentage completed, but realistically if you’re streaming the file (you should), the whole file is treated as a single “chunk” (next_chunk
is only called once), so it’s pretty much useless.
I looked through the docs and, fortunately, there is a lower level MediaIoBaseUpload
class that is less of a blackbox. With this class we can simply hook progress bar callbacks into read
and seek
of an io.IOBase
object. Here’s some skeleton code (where I use an io.BufferedReader
wrapped around an io.FileIO
object, just like what is done by open
):
|
|
Any progress bar class can be hooked into FileReaderWithProgressBar
, with the update method called at places marked as TODO
.
See my upload
script for a complete implementation (commit 111c200
in case the file is moved or refactored in the future).