{"id":1139281,"date":"2025-01-08T22:08:42","date_gmt":"2025-01-08T14:08:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139281.html"},"modified":"2025-01-08T22:08:44","modified_gmt":"2025-01-08T14:08:44","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%ba%93%e7%88%ac%e5%8f%96m3u8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139281.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528\u5e93\u722c\u53d6m3u8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102637\/b78d59ca-ebbd-4b88-a604-ea50ef35c51f.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528\u5e93\u722c\u53d6m3u8\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u4f7f\u7528\u5e93\u722c\u53d6m3u8<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u5e93\u722c\u53d6m3u8\u6587\u4ef6\uff0c\u53ef\u4ee5<strong>\u4f7f\u7528requests\u3001m3u8\u3001ffmpeg<\/strong>\u7b49\u5e93\uff0c\u5206\u522b\u8d1f\u8d23\u7f51\u7edc\u8bf7\u6c42\u3001\u89e3\u6790m3u8\u6587\u4ef6\u3001\u4e0b\u8f7d\u548c\u5408\u5e76\u89c6\u9891\u7247\u6bb5\u3002\u9996\u5148\uff0c\u901a\u8fc7requests\u5e93\u83b7\u53d6m3u8\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528m3u8\u5e93\u89e3\u6790\u51fa\u6240\u6709\u7684.ts\u89c6\u9891\u7247\u6bb5\u94fe\u63a5\u3002\u6700\u540e\uff0c\u5229\u7528ffmpeg\u5e93\u5c06\u8fd9\u4e9b\u89c6\u9891\u7247\u6bb5\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u89c6\u9891\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests m3u8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5e76\u4e14\u9700\u8981\u5b89\u88c5ffmpeg\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># For Ubuntu\/Debian-based systems<\/p>\n<p>sudo apt-get install ffmpeg<\/p>\n<h2><strong>For macOS using Homebrew<\/strong><\/h2>\n<p>brew install ffmpeg<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u83b7\u53d6m3u8\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6m3u8\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u6765\u83b7\u53d6m3u8\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_m3u8_content(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    response.r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se_for_status()  # \u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/p>\n<p>    return response.text<\/p>\n<p>m3u8_url = &#39;http:\/\/example.com\/path\/to\/your.m3u8&#39;<\/p>\n<p>m3u8_content = get_m3u8_content(m3u8_url)<\/p>\n<p>print(m3u8_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790m3u8\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528m3u8\u5e93\u6765\u89e3\u6790m3u8\u6587\u4ef6\uff0c\u63d0\u53d6\u51fa\u6240\u6709.ts\u89c6\u9891\u7247\u6bb5\u7684URL\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import m3u8<\/p>\n<p>def parse_m3u8(content):<\/p>\n<p>    m3u8_obj = m3u8.loads(content)<\/p>\n<p>    ts_urls = [segment.uri for segment in m3u8_obj.segments]<\/p>\n<p>    return ts_urls<\/p>\n<p>ts_urls = parse_m3u8(m3u8_content)<\/p>\n<p>print(ts_urls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0b\u8f7d\u89c6\u9891\u7247\u6bb5<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u4e0b\u8f7d\u6240\u6709\u7684.ts\u89c6\u9891\u7247\u6bb5\uff0c\u5e76\u5c06\u5b83\u4eec\u4fdd\u5b58\u5230\u672c\u5730\u78c1\u76d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def download_ts_segments(ts_urls, save_dir=&#39;videos&#39;):<\/p>\n<p>    if not os.path.exists(save_dir):<\/p>\n<p>        os.makedirs(save_dir)<\/p>\n<p>    for i, url in enumerate(ts_urls):<\/p>\n<p>        response = requests.get(url)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        ts_path = os.path.join(save_dir, f&#39;segment_{i}.ts&#39;)<\/p>\n<p>        with open(ts_path, &#39;wb&#39;) as f:<\/p>\n<p>            f.write(response.content)<\/p>\n<p>        print(f&#39;Downloaded {ts_path}&#39;)<\/p>\n<p>download_ts_segments(ts_urls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5408\u5e76\u89c6\u9891\u7247\u6bb5<\/h3>\n<\/p>\n<p><p>\u4e0b\u8f7d\u6240\u6709\u89c6\u9891\u7247\u6bb5\u540e\uff0c\u4f7f\u7528ffmpeg\u5de5\u5177\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def merge_ts_segments(save_dir=&#39;videos&#39;, output_file=&#39;output.mp4&#39;):<\/p>\n<p>    ts_files = [os.path.join(save_dir, f&#39;segment_{i}.ts&#39;) for i in range(len(ts_urls))]<\/p>\n<p>    ts_file_list = os.path.join(save_dir, &#39;file_list.txt&#39;)<\/p>\n<p>    with open(ts_file_list, &#39;w&#39;) as f:<\/p>\n<p>        for ts_file in ts_files:<\/p>\n<p>            f.write(f&quot;file &#39;{ts_file}&#39;\\n&quot;)<\/p>\n<p>    subprocess.run([&#39;ffmpeg&#39;, &#39;-f&#39;, &#39;concat&#39;, &#39;-<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>&#39;, &#39;0&#39;, &#39;-i&#39;, ts_file_list, &#39;-c&#39;, &#39;copy&#39;, output_file])<\/p>\n<p>merge_ts_segments()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u9519\u8bef\u5904\u7406\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u8003\u8651\u4e00\u4e9b\u9519\u8bef\u5904\u7406\u548c\u4f18\u5316\u7b56\u7565\uff0c\u4f8b\u5982\u91cd\u8bd5\u673a\u5236\u3001\u5e76\u53d1\u4e0b\u8f7d\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u589e\u52a0\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4ee5\u4fbf\u5728\u4e0b\u8f7d\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6\u80fd\u591f\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def download_ts_segments(ts_urls, save_dir=&#39;videos&#39;):<\/p>\n<p>    if not os.path.exists(save_dir):<\/p>\n<p>        os.makedirs(save_dir)<\/p>\n<p>    for i, url in enumerate(ts_urls):<\/p>\n<p>        try:<\/p>\n<p>            response = requests.get(url)<\/p>\n<p>            response.raise_for_status()<\/p>\n<p>            ts_path = os.path.join(save_dir, f&#39;segment_{i}.ts&#39;)<\/p>\n<p>            with open(ts_path, &#39;wb&#39;) as f:<\/p>\n<p>                f.write(response.content)<\/p>\n<p>            print(f&#39;Downloaded {ts_path}&#39;)<\/p>\n<p>        except requests.RequestException as e:<\/p>\n<p>            print(f&#39;Error downloading {url}: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e76\u53d1\u4e0b\u8f7d<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u52a0\u5feb\u4e0b\u8f7d\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def download_ts_segment(url, save_path):<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        with open(save_path, &#39;wb&#39;) as f:<\/p>\n<p>            f.write(response.content)<\/p>\n<p>        print(f&#39;Downloaded {save_path}&#39;)<\/p>\n<p>    except requests.RequestException as e:<\/p>\n<p>        print(f&#39;Error downloading {url}: {e}&#39;)<\/p>\n<p>def download_ts_segments_concurrently(ts_urls, save_dir=&#39;videos&#39;):<\/p>\n<p>    if not os.path.exists(save_dir):<\/p>\n<p>        os.makedirs(save_dir)<\/p>\n<p>    with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>        for i, url in enumerate(ts_urls):<\/p>\n<p>            ts_path = os.path.join(save_dir, f&#39;segment_{i}.ts&#39;)<\/p>\n<p>            executor.submit(download_ts_segment, url, ts_path)<\/p>\n<p>download_ts_segments_concurrently(ts_urls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u5df2\u7ecf\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u6765\u722c\u53d6m3u8\u6587\u4ef6\u5e76\u4e0b\u8f7d\u5176\u89c6\u9891\u7247\u6bb5\uff0c\u6700\u7ec8\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u89c6\u9891\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u57fa\u4e8em3u8\u7684\u6d41\u5a92\u4f53\u89c6\u9891\u4e0b\u8f7d\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u4f18\u5316\u548c\u8c03\u6574\uff0c\u4f8b\u5982\u589e\u52a0\u66f4\u591a\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u4e0b\u8f7d\u65b9\u6cd5\u7b49\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u9ad8\u6548\u722c\u53d6m3u8\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u722c\u53d6m3u8\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u5229\u7528requests\u548cBeautifulSoup\u5e93\u6765\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u89e3\u6790\u51fam3u8\u94fe\u63a5\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u627e\u5230m3u8\u6587\u4ef6\u7684\u94fe\u63a5\uff0c\u7136\u540e\u901a\u8fc7requests\u5e93\u4e0b\u8f7d\u8be5\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u722c\u53d6\u65f6\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u534f\u8bae\uff0c\u4ee5\u514d\u8fdd\u53cd\u7f51\u7ad9\u89c4\u5b9a\u3002<\/p>\n<p><strong>\u722c\u53d6m3u8\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u722c\u53d6m3u8\u6587\u4ef6\u65f6\uff0c\u9700\u6ce8\u610f\u7f51\u7ad9\u7684\u53cd\u722c\u673a\u5236\u3002\u6709\u4e9b\u7f51\u7ad9\u4f1a\u4f7f\u7528\u52a8\u6001\u52a0\u8f7d\u6216\u52a0\u5bc6\u624b\u6bb5\u6765\u4fdd\u62a4m3u8\u94fe\u63a5\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u6216\u8005\u5206\u6790\u7f51\u7edc\u8bf7\u6c42\uff0c\u83b7\u53d6\u771f\u6b63\u7684m3u8\u94fe\u63a5\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5bf9\u670d\u52a1\u5668\u8bf7\u6c42\u9891\u7387\u8fdb\u884c\u63a7\u5236\uff0c\u4ee5\u907f\u514d\u88ab\u5c01\u7981\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u722c\u53d6\u4e0b\u6765\u7684m3u8\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u722c\u53d6m3u8\u6587\u4ef6\uff0c\u63a5\u4e0b\u6765\u53ef\u4ee5\u4f7f\u7528ffmpeg\u7b49\u5de5\u5177\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06m3u8\u6587\u4ef6\u4e2d\u7684\u89c6\u9891\u6d41\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u89c6\u9891\u6587\u4ef6\u3002\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u9700\u786e\u4fddffmpeg\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u76f8\u5e94\u7684\u6307\u4ee4\u3002\u5bf9\u4e8e\u9700\u8981\u4e0b\u8f7d\u7684\u5206\u7247\u6587\u4ef6\uff0c\u53ef\u4ee5\u7f16\u5199Python\u811a\u672c\u81ea\u52a8\u4e0b\u8f7d\uff0c\u786e\u4fdd\u89c6\u9891\u6d41\u7684\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u4f7f\u7528\u5e93\u722c\u53d6m3u8 \u8981\u4f7f\u7528Python\u5e93\u722c\u53d6m3u8\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u3001m3u8\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1139287,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139281"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1139281"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139281\/revisions"}],"predecessor-version":[{"id":1139288,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139281\/revisions\/1139288"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139287"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}