Skip to content

Instantly share code, notes, and snippets.

@NamPNQ
Created November 8, 2024 23:10
Show Gist options
  • Save NamPNQ/bb830047371b143def097042317aac87 to your computer and use it in GitHub Desktop.
Save NamPNQ/bb830047371b143def097042317aac87 to your computer and use it in GitHub Desktop.
yt-dlp fix m3u8 that hides video streams inside PNGs
diff --git a/README.md b/README.md
index 2df72b749..e3ebd1232 100644
--- a/README.md
+++ b/README.md
@@ -526,6 +526,7 @@ ## Download Options:
downloading is finished
--no-keep-fragments Delete downloaded fragments after
downloading is finished (default)
+ --fix-fake-header Fix fake header
--buffer-size SIZE Size of download buffer, e.g. 1024 or 16K
(default is 1024)
--resize-buffer The buffer size is automatically resized
diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py
index a7665159b..9d5f883da 100644
--- a/yt_dlp/__init__.py
+++ b/yt_dlp/__init__.py
@@ -846,6 +846,7 @@ def parse_options(argv=None):
'retry_sleep_functions': opts.retry_sleep,
'skip_unavailable_fragments': opts.skip_unavailable_fragments,
'keep_fragments': opts.keep_fragments,
+ 'fix_fake_header': opts.fix_fake_header,
'concurrent_fragment_downloads': opts.concurrent_fragment_downloads,
'buffersize': opts.buffersize,
'noresizebuffer': opts.noresizebuffer,
diff --git a/yt_dlp/downloader/external.py b/yt_dlp/downloader/external.py
index 6c1ec403c..8e467aedc 100644
--- a/yt_dlp/downloader/external.py
+++ b/yt_dlp/downloader/external.py
@@ -181,7 +181,11 @@ def _call_downloader(self, tmpfilename, info_dict):
continue
self.report_error(f'Unable to open fragment {frag_index}; {err}')
return -1
- dest.write(decrypt_fragment(fragment, src.read()))
+ frag_content = src.read()
+ if self.params.get('fix_fake_header'):
+ ts_start_pos = frag_content.find(b'\x47\x40')
+ frag_content = frag_content[ts_start_pos:]
+ dest.write(decrypt_fragment(fragment, frag_content))
src.close()
if not self.params.get('keep_fragments', False):
self.try_remove(encodeFilename(fragment_filename))
diff --git a/yt_dlp/downloader/fragment.py b/yt_dlp/downloader/fragment.py
index 0d00196e2..f79839c1f 100644
--- a/yt_dlp/downloader/fragment.py
+++ b/yt_dlp/downloader/fragment.py
@@ -141,6 +141,9 @@ def _read_fragment(self, ctx):
raise
ctx['fragment_filename_sanitized'] = frag_sanitized
frag_content = down.read()
+ if self.params.get('fix_fake_header'):
+ ts_start_pos = frag_content.find(b'\x47\x40')
+ frag_content = frag_content[ts_start_pos:]
down.close()
return frag_content
diff --git a/yt_dlp/options.py b/yt_dlp/options.py
index 6c6a0b3f9..d36761be6 100644
--- a/yt_dlp/options.py
+++ b/yt_dlp/options.py
@@ -973,6 +973,10 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
'--no-keep-fragments',
action='store_false', dest='keep_fragments',
help='Delete downloaded fragments after downloading is finished (default)')
+ downloader.add_option(
+ '--fix-fake-header',
+ action='store_true', dest='fix_fake_header', default=False,
+ help='Fix fake header')
downloader.add_option(
'--buffer-size',
dest='buffersize', metavar='SIZE', default='1024',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment