Browse Source

Attempt to add a progress bar to youtube upload

This does not work, at least on Windows. Apparently after the first 
chunck is done, the program hangs and never ask for a next chunck. No 
noticeable network upload is done believing my router.
progress-youtube
Zykino 3 years ago
parent
commit
1639a7bd2d
1 changed files with 53 additions and 4 deletions
  1. +53
    -4
      prismedia/yt_upload.py

+ 53
- 4
prismedia/yt_upload.py View File

@ -15,6 +15,7 @@ import datetime
import pytz import pytz
import logging import logging
from tzlocal import get_localzone from tzlocal import get_localzone
from clint.textui.progress import Bar as ProgressBar
from googleapiclient.discovery import build from googleapiclient.discovery import build
from googleapiclient.errors import HttpError from googleapiclient.errors import HttpError
@ -137,7 +138,7 @@ def initialize_upload(youtube, options):
} }
} }
# If peertubeAt exists, use instead of publishAt
# If youtubeAt exists, use instead of publishAt
if options.get('--youtubeAt'): if options.get('--youtubeAt'):
publishAt = options.get('--youtubeAt') publishAt = options.get('--youtubeAt')
elif options.get('--publishAt'): elif options.get('--publishAt'):
@ -168,7 +169,7 @@ def initialize_upload(youtube, options):
insert_request = youtube.videos().insert( insert_request = youtube.videos().insert(
part=','.join(list(body.keys())), part=','.join(list(body.keys())),
body=body, body=body,
media_body=MediaFileUpload(path, chunksize=-1, resumable=True)
media_body=MediaFileUpload(path, chunksize=-1, resumable=True) # 256 * 1024 * 1024
) )
video_id = resumable_upload(insert_request, 'video', 'insert', options) video_id = resumable_upload(insert_request, 'video', 'insert', options)
@ -295,13 +296,26 @@ def resumable_upload(request, resource, method, options):
error = None error = None
retry = 0 retry = 0
logger_stdout = None logger_stdout = None
if options.get('--url-only') or options.get('--batch'): if options.get('--url-only') or options.get('--batch'):
logger_stdout = logging.getLogger('stdoutlogs') logger_stdout = logging.getLogger('stdoutlogs')
template = 'Youtube: Uploading %s...'
logger.info(template % resource)
# progress_callback = create_callback(100, "percentage") # options.get('--progress'))
while response is None: while response is None:
try: try:
template = 'Youtube: Uploading %s...'
logger.info(template % resource)
status, response = request.next_chunk() status, response = request.next_chunk()
# https://googleapis.github.io/google-api-python-client/docs/media.html
if status:
# progress_callback(int(status.progress() * 100.0))
print("Uploaded %d%%." % int(status.progress() * 100))
else:
print("No status but a loop has been done")
if response is not None: if response is not None:
if method == 'insert' and 'id' in response: if method == 'insert' and 'id' in response:
logger.info('Youtube: Video was successfully uploaded.') logger.info('Youtube: Video was successfully uploaded.')
@ -342,6 +356,41 @@ def resumable_upload(request, resource, method, options):
time.sleep(sleep_seconds) time.sleep(sleep_seconds)
# upload_finished = False
def create_callback(filesize, progress_type):
upload_size_MB = filesize * (1 / (1024 * 1024))
if progress_type is None or "percentage" in progress_type.lower():
progress_lambda = lambda x: int((x / filesize) * 100) # Default to percentage
elif "bigfile" in progress_type.lower():
progress_lambda = lambda x: x * (1 / (1024 * 1024)) # MB
elif "accurate" in progress_type.lower():
progress_lambda = lambda x: x * (1 / (1024)) # kB
else:
# Should not happen outside of development when adding partly a progress type
logger.critical("Youtube: Unknown progress type `" + progress_type + "`")
exit(1)
bar = ProgressBar(expected_size=progress_lambda(filesize), label=f"Youtube upload progress ({upload_size_MB:.2f}MB) ", filled_char='=')
def callback(current_position):
# We want the condition to capture the varible from the parent scope, not a local variable that is created after
# global upload_finished
progress = progress_lambda(current_position)
bar.show(progress)
if current_position == filesize:
# if not upload_finished:
# # We get two time in the callback with both bytes equals, skip the first
# upload_finished = True
# else:
# Print a blank line to not (partly) override the progress bar
print()
logger.info("Youtube: Upload finish, Processing…")
return callback
def hearthbeat(): def hearthbeat():
"""Use the minimums credits possibles of the API so google does not readuce to 0 the allowed credits. """Use the minimums credits possibles of the API so google does not readuce to 0 the allowed credits.
This apparently happens after 90 days without any usage of credits. This apparently happens after 90 days without any usage of credits.

Loading…
Cancel
Save