Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Last active March 25, 2021 17:24
Show Gist options
  • Save djeikyb/c6255775d70cee9bf5ff8bc94391b0f8 to your computer and use it in GitHub Desktop.
Save djeikyb/c6255775d70cee9bf5ff8bc94391b0f8 to your computer and use it in GitHub Desktop.
interface Stream {
file: string;
kind: "video" | "audio";
label?: string;
}
interface Job {
streams: Stream[];
dest: string;
}
const job: Job = {
dest: "out",
streams: [
{
file: "hd720.mp4",
kind: "video",
},
{
file: "hd1080.mp4",
kind: "video",
},
{
file: "2k.mp4",
kind: "video",
},
{
file: "4k.mp4",
kind: "video",
},
],
};
function stripFileExtension(name: string) {
const idx = name.lastIndexOf(".");
return name.slice(0, idx);
}
const args: string[] = [];
// packager docs use this term, "stream descriptor"
// each line that specifies a file input is a stream descriptor
// the format is <field>=<value>[,<field>=<value>]...
for (const stream of job.streams) {
const segmentFolder: string = stripFileExtension(stream.file);
const fields: string[] = [];
fields.push(`in=${stream.file}`);
fields.push(`stream=${stream.kind}`);
fields.push(`playlist_name=${segmentFolder}/stream.m3u8`);
fields.push(`init_segment=${job.dest}/${segmentFolder}/init.mp4`);
fields.push(`segment_template=${job.dest}/${segmentFolder}/$Number$.m4s`);
const stream_descriptor = fields.join(",");
args.push(`'${stream_descriptor}'`);
}
args.push("--hls_playlist_type VOD");
args.push(`--hls_master_playlist_output=${job.dest}/main.m3u8`);
// If segment_template is specified in stream descriptors, shaka-packager
// generates dynamic mpd by default; if this flag is enabled,
// shaka-packager generates static mpd instead
args.push("--generate_static_live_mpd");
args.push(`--mpd_output=${job.dest}/main.mpd`);
const cmd = `rm -r out ; packager \\\n ${args.join(" \\\n ")}`;
console.log(cmd);
@djeikyb
Copy link
Author

djeikyb commented Mar 24, 2021

Download this file to, I dunno, hullo.ts and run it:

deno run hullo.ts

Run the generated shaka packager command (cd to /media first):

docker run --rm -v "$PWD":/media -it --name packager google/shaka-packager

In the same directory as the script, generate the test content like:

ffmpeg -f lavfi -i testsrc=size=hd720 -t 30 -pix_fmt yuv420p hd720.mp4
ffmpeg -f lavfi -i testsrc=size=hd1080 -t 30 -pix_fmt yuv420p hd1080.mp4
ffmpeg -f lavfi -i testsrc=size=2k -t 30 -pix_fmt yuv420p 2k.mp4
ffmpeg -f lavfi -i testsrc=size=4k -t 30 -pix_fmt yuv420p 4k.mp4

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