Skip to content

Instantly share code, notes, and snippets.

@drowsy-probius
Last active November 16, 2023 04:41
Show Gist options
  • Save drowsy-probius/10f2ccac7061ca2a2e4a2ffff1b7bdcd to your computer and use it in GitHub Desktop.
Save drowsy-probius/10f2ccac7061ca2a2e4a2ffff1b7bdcd to your computer and use it in GitHub Desktop.
Tewind m3u8 downloader
// ==UserScript==
// @name Download tewind m3u8
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://tewind.kr/rewind/*
// @match https://tewind.kr/liveback/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tewind.kr
// @grant none
// ==/UserScript==
(() => {
const downloadPlaylist = () =>{
alert('m3u8 다운로드를 시도합니다.', true);
if(!liveback) {
alert('작동하지 않네요');
return;
}
liveback.downloadPlaylist = async function () {
const donwloadBlob = (function () {
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (objectUrl, fileName) {
try {
a.href = objectUrl;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(objectUrl)
} catch (_) {
alert('파일 다운로드 에러');
}
};
}());
let infor = await server.jsonRequest(`/API/vod/${this.type}`, server.makeParam({
key: this.key
}))
if (infor.response !== 200) {
alert(infor.message);
return;
}
const playlistFilenameRaw = `[${infor.content.channel}][${infor.content.category}] ${infor.content.title}`;
const playlistFilename = `${playlistFilenameRaw.slice(0, 250)}.m3u8`;
switch (this.type) {
case 'liveback':{
if (infor.content.tsDuration == null) {
alert('아직 동영상 처리 ');
return;
}
let m3u8 = [
'#EXTM3U',
'#EXT-X-VERSION:3',
'#EXT-X-PLAYLIST-TYPE:VOD',
'#EXT-X-TARGETDURATION:6',
'#EXT-X-MEDIA-SEQUENCE:0'
]
for (let i = 0; i < infor.content.list.length; i++) {
m3u8.push(`#EXTINF:${infor.content.tsDuration}`);
m3u8.push(`${env.cdn.segment}/${infor.content.bid}/${infor.content.list[i]}.ts`);
}
m3u8.push('#EXT-X-ENDLIST');
const obj = new Blob([m3u8.join('\n')])
donwloadBlob(URL.createObjectURL(obj), playlistFilename);
//URL.revokeObjectURL(objectURL)
break;
}
case 'rewind':{
const playlistUrl = `${env.cdn.segment}/${this.key}/${infor.content.src}`;
const res = await fetch(playlistUrl);
const content = await res.blob();
donwloadBlob(URL.createObjectURL(content), playlistFilename);
break;
}
}
};
liveback.downloadPlaylist();
}
const buttonParent = document.getElementById('streamInfo');
const playlistDownloadButton = document.createElement('button');
playlistDownloadButton.id = 'downloadPlaylist';
playlistDownloadButton.onclick = downloadPlaylist
playlistDownloadButton.classList = "button primary small display";
playlistDownloadButton.style = "width: 100px; float: right; "
playlistDownloadButton.innerText = 'm3u8';
buttonParent.appendChild(playlistDownloadButton);
})();
@drowsy-probius
Copy link
Author

drowsy-probius commented May 22, 2023

다운로드한 m3u8 파일은 팟플레이어 등의 동영상 플레이어로 열 수 있음.

ffmpeg를 이용해서 동영상으로 다운받으려면 아래 커맨드를 활용할 수 있음.

ffmpeg -protocol_whitelist file,http,https,tcp,tls -i "여기에_m3u8_파일_이름_경로_입력.m3u8" -c copy "여기에_다운받을_파일_이름_경로_입력.ts"

아래는 윈도우 탐색기에서 드래그 & 드롭으로 m3u8 -> ts로 다운받아주는 bat스크립트

set target=%~1

ffmpeg -protocol_whitelist file,http,https,tcp,tls -i "%target%" -c copy "%target%.ts"

PAUSE 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment