Skip to content

Instantly share code, notes, and snippets.

@Krazete
Created April 25, 2021 22:34
Show Gist options
  • Save Krazete/3d500222e255a7d2a182dec731115daf to your computer and use it in GitHub Desktop.
Save Krazete/3d500222e255a7d2a182dec731115daf to your computer and use it in GitHub Desktop.
Download an MP4 file from a master.json's list of M4S files.
function genBash(masterURL) {
var xhr = new XMLHttpRequest();
xhr.open("GET", masterURL, true);
xhr.addEventListener("load", function () {
var master = JSON.parse(this.responseText);
var baseURL = new URL(master.base_url, masterURL);
function genBashLines(mp4, type) {
var mp4BaseURL = new URL(mp4.base_url, baseURL);
var segmentURLs = mp4.segments.map(segment => segment.url);
return [`base64 -d <<< ${mp4.init_segment} > ${type}.mp4`].concat(
mp4.segments.map(segment => `curl "${new URL(segment.url, mp4BaseURL)}" >> ${type}.mp4`)
);
}
var bestVideo = master.video.sort((a, b) => b.bitrate - a.bitrate)[0];
var bestAudio = master.audio.sort((a, b) => b.bitrate - a.bitrate)[0];
var bashVideo = genBashLines(bestVideo, "video");
var bashAudio = genBashLines(bestAudio, "audio");
var bash = bashVideo.concat(bashAudio);
bash.push("ffmpeg -i video.mp4 -i audio.mp4 -c copy vod.mp4");
bash.push("rm video.mp4");
bash.push("rm audio.mp4");
bash.push("rm genVOD.sh");
var a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([bash.join("\n")], {type: "text/x-shellscript"}));
a.download = "genVOD.sh";
a.click();
});
xhr.send();
}
// genBash("master.json?base64_init=1");
/*
Instructions
1. Open Developer Tools and switch to the Network tab.
2. Play the video to start loading video files. You may pause immediately.
3. Search the Network tab for `master.json`. Copy the URL.
4. Run `genBash(URL)` to download `genVOD.sh`.
5. In the Terminal, navigate to the folder with the downloaded bash file.
6. Run `bash genVOD.sh` to download `vod.mp4`.
Notes
- This is based on Vimeo-hosted videos; specifically, for Moment House.
- The best files are determined via bitrate.
- This script should work if the best files' segments are M4S files.
- If the best files' segments are MP4 files, just download the MP4 without the query parameters.
- Edit the genVOD.sh file to skip the corresponding segment downloads and to adjust merging.
*/
{
"clip_id": "a0b1c2d3-e4f5-g6h7-i8j9-k0l1m2n3o4p5",
"base_url": "../../../",
"video": [
{
"id": "q6r7s8t9",
"base_url": "q6r7s8t9/chop/",
"format": "mp42",
"mime_type": "video/mp4",
"codecs": "avc1.640028",
"bitrate": 5849000,
"avg_bitrate": 4324000,
"duration": 24,
"framerate": 30,
"width": 1920,
"height": 1080,
"max_segment_duration": 6,
"init_segment": "dmlkZW8=",
"segments": [
{"start": 0, "end": 6, "url": "segment-1.m4s", "size": 1933385},
{"start": 6, "end": 12, "url": "segment-2.m4s", "size": 2387826},
{"start": 12, "end": 18, "url": "segment-3.m4s", "size": 1369615},
{"start": 18, "end": 24, "url": "segment-4.m4s", "size": 2065895}
]
}
],
"audio": [
{
"id": "u0v1w2x3",
"base_url": "../audio/u0v1w2x3/chop/",
"format": "mp42",
"mime_type": "audio/mp4",
"codecs": "mp4a.40.2",
"bitrate": 255000,
"avg_bitrate": 255000,
"duration": 24,
"channels": 2,
"sample_rate": 48000,
"max_segment_duration": 6,
"init_segment": "YXVkaW8=",
"segments": [
{"start": 0, "end": 6, "url": "segment-1.m4s", "size": 193189},
{"start": 6, "end": 12, "url": "segment-2.m4s", "size": 193869},
{"start": 12, "end": 18, "url": "segment-3.m4s", "size": 192506},
{"start": 18, "end": 24, "url": "segment-4.m4s", "size": 193189}
]
}
]
}
@Krazete
Copy link
Author

Krazete commented Apr 26, 2021

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