def do_download(self, args): """ download video from youtube and convert to mp3 """ arg_parser = argparse.ArgumentParser( prog="download", description='download audio from youtube') arg_parser.add_argument('url', type=str, metavar='[url]', help='youtube url (video or playlist)') arg_parser.add_argument('--playlist', dest='playlist', default=False, action='store_true', help='download the whole playlist or not') try: args = arg_parser.parse_args(shlex.split(args)) except: return if not self.config.require_one('path'): return if not args.url: arg_parser.print_help() elif args.playlist: try: videos = youtube.get_playlist_videos(args.url) except IndexError: color.display_messages('url is not a playlist', error=True) return for video_url in videos: youtube.download_audio(video_url, self.config['path']) else: youtube.download_audio(args.url, self.config['path'])
def download_audio(video_url, base_dir): try: filename = download_video(video_url, base_dir) mp4_to_mp3(filename, base_dir) os.remove(os.path.join(base_dir, filename)) color.display_messages('audio downloaded succesfully', success=True) except Exception as e: color.display_messages('{} {}'.format(video_url, str(e)), error=True)
def mp4_to_mp3(filename, base_dir): color.display_messages('converting to mp3 - {}'.format(filename), info=True) subprocess.call([ 'ffmpeg', '-hide_banner', '-loglevel', 'error', '-i', os.path.join(base_dir, filename), os.path.join(base_dir, filename[:-3]+'mp3') ])
def do_help(self, args): """ show this help """ names = self.get_names() cmds_doc = [] names.sort() color.display_messages('Available Commands:', info=True, sublime=True) for name in names: if name[:3] == 'do_': cmd = name[3:] if getattr(self, name).__doc__: cmds_doc.append((cmd, getattr(self, name).__doc__)) else: cmds_doc.append((cmd, "")) #self.stdout.write('%s\n'%str(self.doc_header)) self.stdout.write(' {} {}\n'.format('Commands', 'Description')) self.stdout.write(' {} {}\n'.format('--------', '-----------')) for command, doc in cmds_doc: self.stdout.write(' {:<10} {}\n'.format(command, doc)) color.linefeed()
def require_one(self, var, section='main'): if not self.is_set(var, section): color.display_messages('you have to configure the {}'.format(var), error=True) return False return True
def download_video(video_url, base_dir): video = select_video_streaming(video_url) color.display_messages('downloading video - {}'.format(video_url), info=True) video.download(base_dir) return video.default_filename
def do_update(self, args): """ find newer versions """ color.display_messages('checking updates...', info=True) color.display_messages(check_output(['git', 'pull']), info=True)
def upload_audio(filename, path, dest): subprocess.call(['adb', 'push', os.path.join(path, filename), dest]) color.display_messages('audio file uploaded', success=True)