From 0a88e784c28259b252bbae766ecbe46bed1ef2cd Mon Sep 17 00:00:00 2001 From: sepro Date: Sun, 24 Nov 2024 10:45:13 +0100 Subject: [PATCH] Apply suggestions from code review Co-authored-by: bashonly <88596187+bashonly@users.noreply.github.com> --- yt_dlp/YoutubeDL.py | 10 +++++----- yt_dlp/downloader/common.py | 2 +- yt_dlp/extractor/adn.py | 2 +- yt_dlp/extractor/arcpublishing.py | 2 +- yt_dlp/extractor/common.py | 6 +++--- yt_dlp/extractor/livestream.py | 2 +- yt_dlp/extractor/rts.py | 2 +- yt_dlp/extractor/xminus.py | 2 +- yt_dlp/extractor/xstream.py | 2 +- yt_dlp/utils/_utils.py | 12 ++++++------ yt_dlp/webvtt.py | 2 +- 11 files changed, 22 insertions(+), 22 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 672d7e9a0..880a4955d 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -1654,7 +1654,7 @@ def _wait_for_video(self, ie_result={}): or ie_result.get('formats') or ie_result.get('url')): return - format_dur = lambda dur: '{:02}:{:02}:{:02}'.format(*timetuple_from_msec(dur * 1000)[:-1]) + format_dur = lambda dur: '{:02.0f}:{:02.0f}:{:02.0f}'.format(*timetuple_from_msec(dur * 1000)[:-1]) last_msg = '' def progress(msg): @@ -3817,7 +3817,7 @@ def _format_note(self, fdict): if fdict.get('tbr') is not None: if res: res += ', ' - res += '{:4d}k'.format(fdict['tbr']) + res += '{:4.0f}k'.format(fdict['tbr']) if fdict.get('container') is not None: if res: res += ', ' @@ -3832,7 +3832,7 @@ def _format_note(self, fdict): elif fdict.get('vbr') is not None and fdict.get('abr') is not None: res += 'video@' if fdict.get('vbr') is not None: - res += '{:4d}k'.format(fdict['vbr']) + res += '{:4.0f}k'.format(fdict['vbr']) if fdict.get('fps') is not None: if res: res += ', ' @@ -3849,9 +3849,9 @@ def _format_note(self, fdict): res += ', ' res += 'audio' if fdict.get('abr') is not None: - res += '@{:3d}k'.format(fdict['abr']) + res += '@{:3.0f}k'.format(fdict['abr']) if fdict.get('asr') is not None: - res += ' ({:5d}Hz)'.format(fdict['asr']) + res += ' ({:5.0f}Hz)'.format(fdict['asr']) if fdict.get('filesize') is not None: if res: res += ', ' diff --git a/yt_dlp/downloader/common.py b/yt_dlp/downloader/common.py index 22583acae..2aca01aec 100644 --- a/yt_dlp/downloader/common.py +++ b/yt_dlp/downloader/common.py @@ -125,7 +125,7 @@ def format_seconds(seconds): time = timetuple_from_msec(seconds * 1000) if time.hours > 99: return '--:--:--' - return '{:02d}:{:02d}:{:02d}'.format(*map(int, time[:-1])) + return '{:02.0f}:{:02.0f}:{:02.0f}'.format(*time[:-1]) @classmethod def format_eta(cls, seconds): diff --git a/yt_dlp/extractor/adn.py b/yt_dlp/extractor/adn.py index a30c08b21..a28a5d757 100644 --- a/yt_dlp/extractor/adn.py +++ b/yt_dlp/extractor/adn.py @@ -134,7 +134,7 @@ def _get_subtitles(self, sub_url, video_id): ssa += os.linesep + 'Dialogue: Marked=0,{},{},Default,,0,0,0,,{}{}'.format( ass_subtitles_timecode(start), ass_subtitles_timecode(end), - f'{{\\a{alignment}}}' if alignment != 2 else '', + '{\\a%d}' % alignment if alignment != 2 else '', # noqa: UP031 text.replace('\n', '\\N').replace('', '{\\i1}').replace('', '{\\i0}')) if sub_lang == 'vostf': diff --git a/yt_dlp/extractor/arcpublishing.py b/yt_dlp/extractor/arcpublishing.py index cce25c194..e2d034a90 100644 --- a/yt_dlp/extractor/arcpublishing.py +++ b/yt_dlp/extractor/arcpublishing.py @@ -118,7 +118,7 @@ def _real_extract(self, url): if isinstance(f['tbr'], float): f['vbr'] = f['tbr'] * 1000 del f['tbr'] - f['format_id'] = 'rtmp-{}'.format(f['vbr']) + f['format_id'] = 'rtmp-{:.0f}'.format(f['vbr']) formats.extend(smil_formats) elif stream_type in ('ts', 'hls'): m3u8_formats = self._extract_m3u8_formats( diff --git a/yt_dlp/extractor/common.py b/yt_dlp/extractor/common.py index d52fa3eb9..d800f70be 100644 --- a/yt_dlp/extractor/common.py +++ b/yt_dlp/extractor/common.py @@ -2469,7 +2469,7 @@ def _parse_smil_formats_and_subtitles( 'url': streamer, 'play_path': src, 'ext': 'flv', - 'format_id': f'rtmp-{rtmp_count if bitrate is None else bitrate}', + 'format_id': f'rtmp-{(rtmp_count if bitrate is None else bitrate):.0f}', 'tbr': bitrate, 'filesize': filesize, 'width': width, @@ -2493,7 +2493,7 @@ def _parse_smil_formats_and_subtitles( if len(m3u8_formats) == 1: m3u8_count += 1 m3u8_formats[0].update({ - 'format_id': f'hls-{m3u8_count if bitrate is None else bitrate}', + 'format_id': f'hls-{(m3u8_count if bitrate is None else bitrate):.0f}', 'tbr': bitrate, 'width': width, 'height': height, @@ -2524,7 +2524,7 @@ def _parse_smil_formats_and_subtitles( formats.append({ 'url': src_url, 'ext': ext or src_ext or 'flv', - 'format_id': f'http-{bitrate or http_count}', + 'format_id': f'http-{(bitrate or http_count):.0f}', 'tbr': bitrate, 'filesize': filesize, 'width': width, diff --git a/yt_dlp/extractor/livestream.py b/yt_dlp/extractor/livestream.py index e7178638d..3bdadf960 100644 --- a/yt_dlp/extractor/livestream.py +++ b/yt_dlp/extractor/livestream.py @@ -100,7 +100,7 @@ def _parse_smil_formats_and_subtitles( furl += '&ssek=' + vn.attrib['clipBegin'] formats.append({ 'url': furl, - 'format_id': f'smil_{tbr}', + 'format_id': f'smil_{tbr:.0f}', 'ext': 'flv', 'tbr': tbr, 'preference': -1000, # Strictly inferior than all other formats? diff --git a/yt_dlp/extractor/rts.py b/yt_dlp/extractor/rts.py index 2ec75cae6..27cfe70d8 100644 --- a/yt_dlp/extractor/rts.py +++ b/yt_dlp/extractor/rts.py @@ -204,7 +204,7 @@ def extract_bitrate(url): ext = media.get('ext') or determine_ext(media_url, 'mp4') format_id = ext if rate: - format_id += f'-{rate}k' + format_id += f'-{rate:.0f}k' formats.append({ 'format_id': format_id, 'url': urljoin(download_base, media_url), diff --git a/yt_dlp/extractor/xminus.py b/yt_dlp/extractor/xminus.py index dc469e65a..e202a7255 100644 --- a/yt_dlp/extractor/xminus.py +++ b/yt_dlp/extractor/xminus.py @@ -61,7 +61,7 @@ def _real_extract(self, url): 'encoded data') h = time.time() / 3600 a = sum(map(int, [compat_ord(c) for c in k])) + int(video_id) + h - video_url = f'http://x-minus.me/dl/minus?id={video_id}&tkn2={a}f{h}' + video_url = f'http://x-minus.me/dl/minus?id={video_id}&tkn2={a}f{h:.0f}' return { 'id': video_id, diff --git a/yt_dlp/extractor/xstream.py b/yt_dlp/extractor/xstream.py index 061e0b659..281212ca5 100644 --- a/yt_dlp/extractor/xstream.py +++ b/yt_dlp/extractor/xstream.py @@ -74,7 +74,7 @@ def _extract_video_info(self, partner_id, video_id): 'app': mobj.group('app'), 'ext': 'flv', 'tbr': tbr, - 'format_id': f'rtmp-{tbr}', + 'format_id': f'rtmp-{tbr:.0f}', }) else: formats.append({ diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index abe60302e..ec0ebb59e 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -927,12 +927,12 @@ def timetuple_from_msec(msec): def formatSeconds(secs, delim=':', msec=False): time = timetuple_from_msec(secs * 1000) if time.hours: - ret = f'{time.hours}{delim}{time.minutes:02d}{delim}{time.seconds:02d}' + ret = f'{time.hours:.0f}{delim}{time.minutes:02.0f}{delim}{time.seconds:02.0f}' elif time.minutes: - ret = f'{time.minutes}{delim}{time.seconds:02d}' + ret = f'{time.minutes:.0f}{delim}{time.seconds:02.0f}' else: - ret = f'{time.seconds}' - return f'{ret}.{time.milliseconds:03d}' if msec else ret + ret = f'{time.seconds:.0f}' + return f'{ret}.{time.milliseconds:03.0f}' if msec else ret def bug_reports_message(before=';'): @@ -3380,12 +3380,12 @@ def parse_dfxp_time_expr(time_expr): def srt_subtitles_timecode(seconds): - return '{:02d}:{:02d}:{:02d},{:03d}'.format(*map(int, timetuple_from_msec(seconds * 1000))) + return '{:02.0f}:{:02.0f}:{:02.0f},{:03.0f}'.format(*timetuple_from_msec(seconds * 1000)) def ass_subtitles_timecode(seconds): time = timetuple_from_msec(seconds * 1000) - return '{:01d}:{:02d}:{:02d}.{:02d}'.format(*map(int, time[:-1]), int(time.milliseconds / 10)) + return '{:01.0f}:{:02.0f}:{:02.0f}.{:02.0f}'.format(*time[:-1], time.milliseconds / 10) def dfxp2srt(dfxp_data): diff --git a/yt_dlp/webvtt.py b/yt_dlp/webvtt.py index 16961aeb3..a89489eea 100644 --- a/yt_dlp/webvtt.py +++ b/yt_dlp/webvtt.py @@ -111,7 +111,7 @@ def _format_ts(ts): Convert an MPEG PES timestamp into a WebVTT timestamp. This will lose sub-millisecond precision. """ - return '{:02d}:{:02d}:{:02d}.{:03d}'.format(*timetuple_from_msec(int((ts + 45) // 90))) + return '{:02.0f}:{:02.0f}:{:02.0f}.{:03.0f}'.format(*timetuple_from_msec(int((ts + 45) // 90))) class Block: