def start_video_download(url): """This function download 640p quality normal video. Arguments: url {string} -- The url of the video from youtube """ logging.debug("Initiating - {}".format(start_video_download.__name__)) timeout = 5 stream_obj = None while (stream_obj == None and timeout > 0): try: stream_obj = get_pafy_stream_obj(url, format='VIDEO') time.sleep(1) timeout -= 1 except OSError: logging.debug("Video is not available in Youtube.") logging.debug("Link: " + url) break except Exception as e: logging.debug("Error occured in new pafy") logging.debug(e) if stream_obj is not None: # slugify title slugify_video_title = TitleSlugify().slugify_for_windows( stream_obj.title + '.' + stream_obj.extension) path_to_download = os.path.join(DOWN_DIR_VIDEO, slugify_video_title) if not os.path.exists(path_to_download): #starting download try: if not os.path.exists(DOWN_DIR_VIDEO): try: logging.debug( "Making Directory: {}".format(DOWN_DIR_VIDEO)) os.makedirs(DOWN_DIR_VIDEO) # os.mkdir(DOWN_DIR_VIDEO) except Exception as e: logging.debug( "Error occured in making Directory: {}".format( TEMP_DIR)) logging.debug(e) logging.debug( "Downloading Video: " + TitleSlugify().slugify_for_windows(stream_obj.title)) logging.debug("Saving to: " + os.path.abspath(DOWN_DIR_VIDEO)) stream_obj.download(filepath=path_to_download) logging.debug("DOWNLOADED=> " + slugify_video_title) notifyAboutTheService("Downloded", slugify_video_title) except Exception as e: notifyAboutTheService("Error Downloading", slugify_video_title) logging.debug("Unable to download. Error occured") logging.debug(e) else: logging.debug("File Already Exists! Path: " + path_to_download) else: logging.debug( "Unable to find the video file at this time. Timeout!! Try again later." )
def stop_or_reset(self): # enabling radiobuttons after pressing stop | reset self.enable_buttons() # handling duplicates buttons clicks is_stopped = stopTheServers() if is_stopped: resetTheThreads() notifyAboutTheService("Server Stopped!", "Download server stopped!")
def start_server(self): #generating config from the radio butons self.generateMediaConfig() # reloading config for the daemon server # reloading download directories reloadConfig() # disabling radiobuttons after starting the server self.disable_buttons() # handling double start server button click # button won't response if server is already running is_alive = startTheServers() if not is_alive: notifyAboutTheService("Server Started", "Download server started successfully!")
def start_high_quality_video_download(url): """ This function download both video and audio separately and combine them to produce 1080p video. Arguments: url {string} -- The url of the video from youtube """ logging.debug("Initiating - {}".format( start_high_quality_video_download.__name__)) # making temp directory if not exist # this folder will be used to temporary storing video and audio files # those temporary files will be deleted once combine operatoin is successfull if not os.path.exists(TEMP_DIR): try: logging.debug("Making Directory: {}".format(TEMP_DIR)) os.makedirs(TEMP_DIR) except Exception as e: logging.debug( "Error occured in making Directory: {}".format(TEMP_DIR)) logging.debug(e) if not os.path.exists(DOWN_DIR_VIDEO): try: logging.debug("Making Directory: {}".format(DOWN_DIR_VIDEO)) os.makedirs(DOWN_DIR_VIDEO) except Exception as e: logging.debug( "Error occured in making Directory: {}".format(DOWN_DIR_VIDEO)) logging.debug(e) # downloaing only video timeout = 5 video = None while video == None and timeout > 0: try: video = get_pafy_stream_obj(url, format='VIDEO', only_video=True) time.sleep(1) except OSError: logging.debug("Video is not available in Youtube.") logging.debug("Link: " + url) break timeout -= 1 if video is not None: # slugifying title slugify_video_title = TitleSlugify().slugify_for_windows( video.title + '.' + video.extension) temp_path_to_download_video = os.path.join(TEMP_DIR, slugify_video_title) output_path = os.path.join(DOWN_DIR_VIDEO, slugify_video_title) if not os.path.exists(output_path): try: logging.debug("Downloading HQ Video: " + TitleSlugify().slugify_for_windows(video.title)) video.download(filepath=temp_path_to_download_video) except Exception as e: logging.debug( "Exception occured at high_quality_video_download video downloader" ) logging.debug(e) # this need to be checked return # downloading only audio timeout = 5 audio = None while audio == None and timeout > 0: try: audio = get_pafy_stream_obj(url, format='AUDIO') time.sleep(1) except OSError: logging.debug("Video is not availble in Youtube.") logging.debug("Link: " + url) break timeout -= 1 # if audio is not avilable # then video is also not available if audio is not None: # slugifying title slugify_audio_title = TitleSlugify().slugify_for_windows( audio.title + '.' + audio.extension) # setting download location temp_path_to_download_audio = os.path.join( TEMP_DIR, slugify_audio_title) try: logging.debug( "Downloading HQ Audio: " + TitleSlugify().slugify_for_windows(audio.title)) audio.download(filepath=temp_path_to_download_audio) except Exception as e: logging.debug( "Exception occured at high_quality_video_download audio downloader" ) logging.debug(e) # combining both video and audio cmd = [ str(FFMPEG_LOCATION), FFMPEG_LOG, FFMPEG_LOG_LEVEL, '-i', temp_path_to_download_video, '-i', temp_path_to_download_audio, '-c', 'copy', '-strict', 'experimental', output_path ] # running command with subprocess try: logging.debug( "Combining HQ Audio and Video: " + TitleSlugify().slugify_for_windows(audio.title)) logging.debug("Saving to: " + os.path.abspath(DOWN_DIR_VIDEO)) subprocess.run(cmd, shell=True) logging.debug("DOWNLOADED=> " + slugify_video_title) notifyAboutTheService("Downloaded", slugify_video_title) except Exception as e: notifyAboutTheService("Error Downloading", slugify_video_title) logging.debug( "Errore occured during runing combining ffmpeg command" ) logging.debug(e) finally: try: os.remove(temp_path_to_download_audio) os.remove(temp_path_to_download_video) except Exception as e: logging.debug( "Unable to remove temporary files in temp folder({})" .format(TEMP_DIR)) logging.debug(e) else: logging.debug("File Already Exists! Path: " + output_path) else: logging.debug( "Unable to find the video file at this time. Timeout!! Try again later." )
def start_audio_download(url): """This is a function to download audio file as m4a form pafy streamobj and also convert them to mp3 Arguments: url {string} -- The url of the video from yoututbe """ logging.debug("Initiating - {}".format(start_audio_download.__name__)) # trying to get stream obj from pafy # until the object is received witout an errore # the while loop keeps reqesting pafy # if video is not available in youtube # gives OSError and breaks the loop timeout = 5 stream_obj = None pafy_obj = None while (stream_obj == None and timeout > 0): try: pafy_obj = get_pafy_stream_obj(url) time.sleep(1) timeout -= 1 except OSError: logging.debug("Video is not available in Youtube.") logging.debug("Link: " + url) break except Exception as e: logging.debug("Error occured in new pafy") logging.debug(e) # sys.exit() if pafy_obj is not None: # getting audio streams from pafy_obj stream_obj = pafy_obj.getbestaudio(preftype='m4a') # slugifying title slugify_audio_title = TitleSlugify().slugify_for_windows( stream_obj.title + '.' + stream_obj.extension) path_to_download = os.path.join(DOWN_DIR_AUDIO, slugify_audio_title) #checking if the file already exists if not os.path.exists(path_to_download) and not os.path.exists( path_to_download.replace('.m4a', '.mp3')): #starting download try: if not os.path.exists(DOWN_DIR_AUDIO): try: logging.debug( "Making Directory: {}".format(DOWN_DIR_AUDIO)) os.makedirs(DOWN_DIR_AUDIO) # os.mkdir(DOWN_DIR_AUDIO) except Exception as e: logging.debug( "Error occured in making Directory: {}".format( DOWN_DIR_AUDIO)) logging.debug(e) logging.debug( "Downloading Audio: " + TitleSlugify().slugify_for_windows(stream_obj.title)) # intiate the download stream_obj.download(filepath=path_to_download) logging.debug("Saving to: " + os.path.abspath(DOWN_DIR_AUDIO)) #converting to mp3 cmd = [ str(FFMPEG_LOCATION), FFMPEG_LOG, FFMPEG_LOG_LEVEL, '-i', path_to_download, '-vn', '-ab', '128k', '-ar', '44100', '-y', os.path.join(DOWN_DIR_AUDIO, slugify_audio_title.replace('.m4a', '.mp3')) ] # logging.debug(" ".join(cmd)) try: logging.debug("Converting m4a to mp3") subprocess.run(cmd, shell=True) logging.debug("DOWNLOADED=> " + slugify_audio_title.replace("m4a", "mp3")) # adding unicode title from stream obj addTitle(path_to_download.replace('m4a', 'mp3'), stream_obj.title) addPicture(path_to_download.replace('m4a', 'mp3'), pafy_obj) notifyAboutTheService( "Downloaded", slugify_audio_title.replace("m4a", "mp3")) except Exception as e: notifyAboutTheService( "Error Downloading", slugify_audio_title.replace("m4a", "mp3")) logging.debug("Errore occured in converting file") logging.debug(e) try: # subprocess.run(['rm',path_to_download]) logging.debug("Removing m4a file") os.remove(path_to_download) except Exception as e: logging.debug("Errore removing actual file") logging.debug(e) except Exception as e: logging.debug("Unable to download. Error occured") logging.debug(e) else: logging.debug("File Already Exists! Path: " + path_to_download) else: logging.debug( "Unable to find the audio file at this time. Timeout!! Try again later or Try updating 'youtube-dl'dependency." )