chocojunkie
chocojunkie

Reputation: 575

Downloading m3u8 stream as mp3

I have a .m3u8 link which I'm trying to download as an .mp3 file.

import m3u8
import subprocess
import requests

link = 'https://cs9-9v4.vkuseraudio.net/s/v1/ac/NeMmHNX2Iyt08MZ4z5fELAMybgSNR6T1xYEcBEv5Kdsenci3KHOAC-1fKapAV9vxwVOBIik40I4DwfrN-a_jtjILYVcx3mLTNCzKo1UF-UhbKOztLrboF9NEn1jzZs1Jl0ijfmccog6aAcB4PcdnrxPzXY7WCMVWtUjWKOgHad5a-g0/index.m3u8'

m3u8_parsed = m3u8.load(link)

with open('track.ts', 'wb') as f:
  for segment in m3u8_parsed.segments:
    r = requests.get(segment.absolute_uri)
    f.write(r.content)

subprocess.run(['ffmpeg', '-i', 'track.ts', 'track.mp3'])

The result is that the track.ts file takes up 6MB storage but doesn't have sound when I try to play it with the VLC Player, while track.mp3 is basically an empty file taking up a couple kilobytes.

Upvotes: 2

Views: 8030

Answers (1)

kesh
kesh

Reputation: 5463

You only need FFmpeg to retrieve the audio:

import subprocess

link = 'https://cs9-9v4.vkuseraudio.net/s/v1/ac/NeMmHNX2Iyt08MZ4z5fELAMybgSNR6T1xYEcBEv5Kdsenci3KHOAC-1fKapAV9vxwVOBIik40I4DwfrN-a_jtjILYVcx3mLTNCzKo1UF-UhbKOztLrboF9NEn1jzZs1Jl0ijfmccog6aAcB4PcdnrxPzXY7WCMVWtUjWKOgHad5a-g0/index.m3u8'

subprocess.run(['ffmpeg', '-i', link, 'track.mp3'])

This resulted in a playable mp3 file when I tried it.

Upvotes: 9

Related Questions