m3u8 Video Downloader

Share your advanced PureBasic knowledge/code with the community.
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

m3u8 Video Downloader

Post by OgreVorbis »

I thought some of you might find this useful or somewhat interesting. There are programs out there such as youtube-dl and many browser extensions to download from video sites. Sometimes I come across a new site that uses m3u8 to store their videos and I don't like to run many untrusted extensions in my browser. I discovered that sometimes it's as easy as fetching the m3u playlist, reading it, and then downloading each video chunk and simply appending it to the end of the main file; this results in a playable mp4 download.
In simple terms, you just need to open a video page (not youtube); open the browser inspection panel; go to network; refresh page (ctrl+ F5); then look at that list for the m3u8 file; copy URL; paste into this console program.

I'd love to make it work more like youtube-dl, where you can just put an actual video link, but turns out this becomes incredibly hard outside of using an embedded browser. It's still pretty fast and convenient though once you get the hang of it. And you don't have to install any bloated or sus stuff.

Pastebin link: https://pastebin.com/89aCs5e6

Code: Select all

Dim RealLines.s(0)
#TempFile = "temp.m3u"

InitNetwork()
OpenConsole("m3u8 Video Downloader")

tmpFile.s = GetTemporaryDirectory() + #TempFile

PrintN("This program will let you download full vids given the m3u8 link.")
PrintN("You get this link by loading a vid in your browser with the inspect panel open.")
PrintN("In the network tab of the inspect panel, you will find the m3u8 link.")
PrintN("You may need to refresh the page.")
PrintN("")
PrintN("Paste your m3u8 URL here:")
theURL.s = Input()
PrintN("")
PrintN("Type your output file name (path or not):")
outputFile.s = Input()

If outputFile = ""
	PrintN("ERROR: Bad file name!")
	Print("Press ENTER to terminate. . .")
	Input()
	CloseConsole()
	End
EndIf
If Right(outputFile, 4) <> ".mp4": outputFile + ".mp4": EndIf
If Mid(outputFile, 2, 1) <> ":": outputFile = GetTemporaryDirectory() + outputFile: EndIf

If ReceiveHTTPFile(theURL, tmpFile)
	Delay(2000)
	OpenFile(0, tmpFile)
	lines = 0
	Repeat
		tmpLine.s = ReadString(0, #PB_UTF8)
		If Left(tmpLine, 1) <> "#"
			RealLines(lines) = tmpLine
			lines + 1
			ReDim RealLines.s(lines)
		EndIf
	Until Eof(0) = #True
	lines - 1
	CloseFile(0)
	Delay(500)
	DeleteFile(tmpFile, #PB_FileSystem_Force)
	
	lastSlash = Len(theURL) - FindString(ReverseString(theURL), "/") + 1 ; get the last slash in URL
	stubURL.s = Left(theURL, lastSlash)
	PrintN("Stub URL: " + stubURL)
	PrintN("======================")
	OpenFile(0, outputFile)
	For i = 0 To lines
		*buffer = ReceiveHTTPMemory(stubURL + RealLines(i))
		If *buffer
			size = MemorySize(*buffer)
			PrintN("Writing chunk " + Str(i) + " of " + Str(lines))
			PrintN(RealLines(i))
			WriteData(0, *buffer, size)
			FreeMemory(*buffer)
		Else
			PrintN("ERROR: Memory problem for this chunk.")
		EndIf
		Delay(500)
	Next
	CloseFile(0)
	PrintN("")
	PrintN("DONE!")
Else
	PrintN("")
	PrintN("ERROR: Could not download m3u8 file!")
EndIf

Print("Press ENTER to terminate. . .")
Input()
CloseConsole()
End
My blog/software site: http://dosaidsoft.com/
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: m3u8 Video Downloader

Post by Mijikai »

Nice, thanks for sharing.
Post Reply