How to Parse M3U8 Files

M3U8 File Parsing Steps

Parsing M3U8 files is the first step in playing HLS streams. The basic workflow involves fetching the M3U8 file content, determining whether it's a Master Playlist or Media Playlist, and extracting segment information for playback.

1. Fetch the M3U8 File

Fetch the M3U8 file content via HTTP request. M3U8 files are plain text files that can be read directly. In browser environments, use fetch API or XMLHttpRequest; in Node.js, use axios or built-in http module.

When fetching M3U8 files, note: use correct HTTP method (usually GET), handle CORS issues, and handle redirects (301/302) as files may be hosted on CDN.

2. Determine File Type

Check if the file contains the #EXT-X-STREAM-INF tag. If yes, it's a Master Playlist requiring bitrate selection; otherwise, it's a Media Playlist ready for playback.

3. Parse Master Playlist

Extract URLs and attributes for each bitrate stream. Each #EXT-X-STREAM-INF tag is followed by a URL pointing to the corresponding Media Playlist. Attributes include BANDWIDTH, RESOLUTION, CODECS, AUDIO, etc.

4. Parse Media Playlist

Extract key information:

  • Segment URL list: Each #EXTINF tag followed by a URL to the TS segment file.
  • Segment duration: From #EXTINF tag, format: #EXTINF:,.</li> <li><strong>Maximum segment duration</strong>: From #EXT-X-TARGETDURATION tag.</li> <li><strong>Media sequence number</strong>: From #EXT-X-MEDIA-SEQUENCE tag.</li> <li><strong>Whether it's live</strong>: Missing #EXT-X-ENDLIST indicates live stream.</li> </ul> <h2>5. Parsing Tools</h2> <p>Common M3U8 parsing tools:</p> <ul> <li><strong>hls.js</strong> - JavaScript library for browser-side parsing and playback.</li> <li><strong>video.js</strong> - Universal video player framework with HLS support.</li> <li><strong>ffprobe</strong> - FFmpeg tool for command-line parsing.</li> <li><strong>m3u8-parser</strong> - Dedicated M3U8 parsing library.</li> </ul></div><div class="article-nav"><a href="/en/post/1008" class=""> ← M3U8 Protocol Principles</a><a href="/en/post/1010" class="">Video Codec Formats Explained → </a></div></article><!--]--></div></main><footer class="app-footer"><div class="footer-grid"><div class="footer-brand"><a href="/en" class="logo" style="margin-bottom:8px;"><img src="/icon.svg" alt="M3U8 Player" class="logo-icon" style="width:32px;height:32px;"><span style="font-weight:700;font-size:18px;color:var(--color-text);">M3U8 Player</span></a><p>Free browser-based M3U8/HLS stream testing tool for developers. Test live streams, debug playback issues, and validate video delivery — powered by hls.js.</p></div><div class="footer-col"><h4>Product</h4><a href="/en/player" class="">Player</a><a href="/en/extension" class="">Extension</a><a href="/en/help" class="">Help</a></div><div class="footer-col"><h4>Resources</h4><a href="/en/post/1001" class="">What is the M3U8 Protocol</a><a href="/en/post/1002" class="">How to Play M3U8 Videos</a><a href="/en/post/1008" class="">M3U8 Protocol Principles</a></div><div class="footer-col"><h4>Support</h4><a href="/en/about" class="">About</a><a href="/en/help" class="">Help</a><a href="/en/contact" class="">Contact</a><a href="mailto:baronheguang@gmail.com">baronheguang@gmail.com</a></div><div class="footer-col"><h4>Legal</h4><a href="/en/privacy" class="">Privacy Policy</a><a href="/en/terms" class="">Terms of Service</a><a href="/en/advertising" class="">Advertising Policy</a></div></div><div class="footer-disclaimer"><p>Usage Notice & Copyright</p><ul><li>This tool may only be used to play M3U8/HLS video links that you are legally authorized to access.</li><li>Using this tool to play or distribute copyrighted or illegal content is strictly prohibited.</li><li>You are solely responsible for all content played through this tool.</li></ul></div><div class="footer-bottom"><p class="footer-copyright">© 2019 - 2026 M3U8 Player. All rights reserved.</p><p class="footer-disclaimer-text">This tool plays M3U8/HLS links provided by users only. We do not store or host any video content.</p><div class="footer-langs"><a href="/post/1009" class="" hreflang="zh">中文</a><a aria-current="page" href="/en/post/1009" class="router-link-active router-link-exact-active" hreflang="en">English</a><a href="/pt/post/1009" class="" hreflang="pt">Português</a><a href="/ja/post/1009" class="" hreflang="ja">日本語</a></div></div></footer><!----></div></div><!--]--></div><div id="teleports"></div><script>window.__NUXT__={};window.__NUXT__.config={public:{siteUrl:"https://www.sensezpe.com",googleAnalyticsId:"G-WV19JPESQ5",emailjsPublicKey:"1Vhp_t59P91Vzm6r2",emailjsServiceId:"service_wijfvpr",emailjsTemplateId:"template_tpjkr4i",i18n:{baseUrl:"",defaultLocale:"zh",rootRedirect:"",redirectStatusCode:302,skipSettingLocaleOnNavigate:false,locales:[{code:"zh",name:"中文",language:"zh-CN"},{code:"en",name:"English",language:"en-US"},{code:"pt",name:"Português",language:"pt-BR"},{code:"ja",name:"日本語",language:"ja-JP"}],detectBrowserLanguage:{alwaysRedirect:false,cookieCrossOrigin:false,cookieDomain:"",cookieKey:"i18n_redirected",cookieSecure:false,fallbackLocale:"",redirectOn:"root",useCookie:true},experimental:{localeDetector:"",typedPages:true,typedOptionsAndMessages:false,alternateLinkCanonicalQueries:true,devCache:false,cacheLifetime:"",stripMessagesPayload:false,preload:false,strictSeo:false,nitroContextDetection:true,httpCacheDuration:10,compactRoutes:false},domainLocales:{zh:{domain:""},en:{domain:""},pt:{domain:""},ja:{domain:""}}}},app:{baseURL:"/",buildId:"35863392-fec2-455f-8f9b-b0b59a8ea2f0",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="true" id="__NUXT_DATA__" data-src="/en/post/1009/_payload.json?35863392-fec2-455f-8f9b-b0b59a8ea2f0">[{"state":1,"once":24,"_errors":25,"serverRendered":6,"path":27,"prerenderedAt":28},["Reactive",2],{"$si18n:cached-locale-configs":3,"$si18n:resolved-locale":13,"$ssite-config":14},{"zh":4,"en":7,"pt":9,"ja":11},{"fallbacks":5,"cacheable":6},[],true,{"fallbacks":8,"cacheable":6},[],{"fallbacks":10,"cacheable":6},[],{"fallbacks":12,"cacheable":6},[],"en",{"_priority":15,"currentLocale":19,"defaultLocale":20,"env":21,"name":22,"url":23},{"name":16,"env":17,"url":16,"defaultLocale":18,"currentLocale":18},-3,-15,-2,"en-US","zh-CN","production","M3U8在线播放器","https:\u002F\u002Fwww.sensezpe.com",["Set"],["ShallowReactive",26],{},"\u002Fen\u002Fpost\u002F1009",1780033594681]</script></body></html>