From 350d3727d5cf7a9439cc7e150c9cc49f45f8f21b Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Sun, 3 Mar 2019 17:14:22 +0800 Subject: [PATCH] v0.3.1 drop http link in local m3u8 file. --- m3u8downloader/__init__.py | 2 +- m3u8downloader/main.py | 32 ++++++++++++++++++++++++++++++++ m3u8downloader/test_main.py | 6 ++++++ operational | 25 +++++++++++++++++++++++++ requirements.txt | 2 +- 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/m3u8downloader/__init__.py b/m3u8downloader/__init__.py index 493f741..260c070 100644 --- a/m3u8downloader/__init__.py +++ b/m3u8downloader/__init__.py @@ -1 +1 @@ -__version__ = "0.3.0" +__version__ = "0.3.1" diff --git a/m3u8downloader/main.py b/m3u8downloader/main.py index 0f012c4..1ea4004 100644 --- a/m3u8downloader/main.py +++ b/m3u8downloader/main.py @@ -96,6 +96,37 @@ def get_fullpath(filename): return os.path.abspath(os.path.expandvars(os.path.expanduser(filename))) +def http_line_to_relpath_line(url_line): + """convert a url line in m3u8 file to use relative path in local fs. + + """ + r = urlparse(url_line) + return r.path[1:] + + +def drop_http_link_in_m3u8_file(local_m3u8_filename): + """drop http protocol, host part for all resource lines. + + """ + with open(local_m3u8_filename, 'r') as f: + content = f.read() + if 'http' not in content: + logger.info("media playlist m3u8 file doesn't contain http link") + return + with open(local_m3u8_filename, 'w') as f: + for line in content.split('\n'): + if line.startswith('#'): + f.write(line) + f.write('\n') + if line.strip() == '': + f.write(line) + f.write('\n') + if line.startswith('http'): + f.write(http_line_to_relpath_line(line)) + f.write('\n') + logger.info("updated media playlist m3u8 file: %s", local_m3u8_filename) + + class M3u8Downloader: def __init__(self, url, output_filename, tempdir="."): self.start_url = url @@ -223,6 +254,7 @@ class M3u8Downloader: """ self.media_playlist_localfile = self.mirror_url_resource(url) + drop_http_link_in_m3u8_file(self.media_playlist_localfile) if content is None: content = get_url_content(url) diff --git a/m3u8downloader/test_main.py b/m3u8downloader/test_main.py index 74feebd..7029b42 100644 --- a/m3u8downloader/test_main.py +++ b/m3u8downloader/test_main.py @@ -12,8 +12,14 @@ from m3u8downloader.main import is_higher_resolution from m3u8downloader.main import get_url_path from m3u8downloader.main import get_basename from m3u8downloader.main import get_fullpath +from m3u8downloader.main import http_line_to_relpath_line +# test for http_line_to_relpath_line +def test_http_line_to_relpath_line(): + assert http_line_to_relpath_line("https://m3u.x8sinablog.net/20180627/1530068509/vts/audio000.png") == "20180627/1530068509/vts/audio000.png" + assert http_line_to_relpath_line("https://m3u.x8sinablog.net/20180627/1530068509/vts/audio000.png?abc=def") == "20180627/1530068509/vts/audio000.png" + # test for join def test_join(): assert os.path.normpath(os.path.join("/foo/bar/baz", "./abc.txt")) == "/foo/bar/baz/abc.txt" diff --git a/operational b/operational index 8bbb880..53df504 100644 --- a/operational +++ b/operational @@ -39,6 +39,31 @@ to handle it on ryzen5. * current :entry: ** +** 2019-03-03 generate mp4 for audio sequence fail. +probably because of png ext. +https://m3u.x8sinablog.net/20180627/1530068509/vts/audio000.png + +ffprobe on the png file show correct result. +it's aac audio. + +- try again. + + ffmpeg -allowed_extensions ALL -i "/home/sylecn/.cache/m3u8downloader/28】/apiv286.m3u8" -acodec copy -vcodec copy -bsf:a aac_adtstoasc ~/d/t2/foo.m4a + + it's not the file extension's problem. + it's the URL link. + +- audio .m3u8 uses absolute link that is not hosted on the same domain. + + media playlist: + ~/.cache/m3u8downloader/28】/apiv286.m3u8 + https://m3u.x8sinablog.net/20180627/1530068509/vts/audio000.png + + I need to truncate the domain part for the local m3u8 files. + plain mirror is not enough. + + it works. + ** 2019-03-03 add progress tracking log. do a commit before I add this. diff --git a/requirements.txt b/requirements.txt index 4824c83..5d398c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ wells==1.4.1 -requests +requests==2.21.0 # six==1.12.0 # raven==6.0.0 -- GitLab