Skip to content

Commit

Permalink
Create msd file format (#39)
Browse files Browse the repository at this point in the history
* Create msd file format
  • Loading branch information
matvp91 authored Oct 10, 2024
1 parent f4902fa commit d6341a1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
13 changes: 8 additions & 5 deletions packages/api/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export const postTranscodeBodySchema = z.object({

export const postPackageBodySchema = z.object({
assetId: z.string(),
segmentSize: z.number().optional().openapi({
default: 4,
description:
"Segment size, must be equal or a multiple of the segmentSize defined in transcode.",
}),
segmentSize: z
.number()
.optional()
.openapi({
description:
"Segment size. When defined, must be equal or a multiple of the segmentSize defined in transcode. " +
"When not defined, will take the original segmentSize from transcode.",
}),
tag: z.string().optional().openapi({
description: "An arbitrary tag, used to group jobs.",
}),
Expand Down
21 changes: 14 additions & 7 deletions packages/artisan/src/consumer/workers/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import { streamSchema } from "@mixwave/shared/artisan";
import type { Job } from "bullmq";
import type { Code } from "iso-language-codes";

const metaSchema = z.record(z.string(), streamSchema);
const metaSchema = z.object({
version: z.number(),
streams: z.record(z.string(), streamSchema),
segmentSize: z.number(),
});

export type PackageData = {
params: {
assetId: string;
segmentSize: number;
segmentSize?: number;
name: string;
};
metadata: {
Expand All @@ -39,16 +43,19 @@ export default async function (job: Job<PackageData, PackageResult>) {
const dir = dirSync();
await downloadFolder(dir.name, `transcode/${params.assetId}`);

const metaFile = await metaSchema.parseAsync(
const meta = await metaSchema.parseAsync(
JSON.parse(await readFile(`${dir.name}/meta.json`, "utf8")),
);

// If we do not specify the segmentSize, grab it from the meta file.
const segmentSize = params.segmentSize ?? meta.segmentSize;

const outDir = dirSync();

const packagerParams: string[][] = [];

for (const key of Object.keys(metaFile)) {
const stream = metaFile[key];
for (const key of Object.keys(meta.streams)) {
const stream = meta.streams[key];
const file = parseFilePath(key);

if (stream.type === "video") {
Expand Down Expand Up @@ -90,9 +97,9 @@ export default async function (job: Job<PackageData, PackageResult>) {

packagerArgs.push(
"--segment_duration",
params.segmentSize.toString(),
segmentSize.toString(),
"--fragment_duration",
params.segmentSize.toString(),
segmentSize.toString(),
"--hls_master_playlist_output",
"master_tmp.m3u8",
);
Expand Down
24 changes: 14 additions & 10 deletions packages/artisan/src/consumer/workers/transcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ export default async function (job: Job<TranscodeData, TranscodeResult>) {

const childrenValues = await fakeJob.getChildrenValues();

const meta = Object.entries(childrenValues).reduce<Record<string, Stream>>(
(acc, [key, value]) => {
if (key.startsWith("bull:ffmpeg")) {
const ffmpegResult: FfmpegResult = value;
acc[ffmpegResult.name] = ffmpegResult.stream;
}
return acc;
},
{},
);
const meta = {
version: 1,
streams: Object.entries(childrenValues).reduce<Record<string, Stream>>(
(acc, [key, value]) => {
if (key.startsWith("bull:ffmpeg")) {
const ffmpegResult: FfmpegResult = value;
acc[ffmpegResult.name] = ffmpegResult.stream;
}
return acc;
},
{},
),
segmentSize: params.segmentSize,
};

await job.log(`Writing meta.json (${JSON.stringify(meta)})`);

Expand Down
2 changes: 1 addition & 1 deletion packages/artisan/src/producer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type AddPackageJobData = {

export async function addPackageJob({
assetId,
segmentSize = 4,
segmentSize,
name = "hls",
tag,
}: AddPackageJobData) {
Expand Down

0 comments on commit d6341a1

Please sign in to comment.