Skip to content

Commit

Permalink
encore profile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Nov 6, 2023
1 parent a700a1b commit 81dea1a
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 120 deletions.
43 changes: 43 additions & 0 deletions examples/encore/encoreExample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { EncorePipelineConfiguration } from '../../src/pipelines/encore/encore-pipeline-configuration';
import { EncorePipeline } from '../../src/pipelines/encore/encore-pipeline';
import createJob from '../../src/create-job';
import fs from 'fs';

async function transcodeInputsAndAnalyze() {

const configuration: EncorePipelineConfiguration = {
apiAddress: "https://api-encore.stage.osaas.io",
token: "",
instanceId: "dummy",
profilesUrl: "profilesUrl",
outputFolder: "/usercontent/demo",
baseName: "_demo",
input: "https://testcontent.eyevinn.technology/mp4/stswe-tvplus-promo.mp4",
duration: 120,
priority: 0,
encorePollingInterval_ms: 30000,
encoreInstancePostCreationDelay_ms: 10000
};

const inlineProfile = fs.readFileSync('encoreProfile.yml', 'utf8');

const resolutions = [{
width: 1280,
height: 720,
range: {
min: 500000,
max: 600000
}
}];

const bitrates = [
500000,
600000,
800000
]

const pipeline: EncorePipeline = new EncorePipeline(configuration);
await pipeline.transcode(configuration.input, { width: 1280, height: 720}, 600000, "output", undefined, inlineProfile, resolutions, bitrates);
}

transcodeInputsAndAnalyze();
17 changes: 17 additions & 0 deletions examples/encore/encoreProfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: X264Encode
description: Program profile
scaling: bicubic
encodes:
- type: X264Encode
suffix: _x264_
twoPass: true
height:
width:
params:
b:v:
maxrate:
minrate:
r: 25
fps_mode: cfr
pix_fmt: yuv420p
force_key_frames: expr:not(mod(n,96))
12 changes: 12 additions & 0 deletions examples/encore/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
encore:
apiAddress: https://api-encore.stage.osaas.io,
token: null,
instanceId: dummy,
profilesUrl: profilesUrl,
outputFolder: /usercontent/demo,
baseName: _demo,
input: https://testcontent.eyevinn.technology/mp4/stswe-tvplus-promo.mp4,
duration: 120,
priority: 0,
encorePollingInterval_ms: 30000,
encoreInstancePostCreationDelay_ms: 10000,
28 changes: 0 additions & 28 deletions examples/encoreExample.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/analysis/brute-force.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export default async function analyzeBruteForce(directory: string, reference: st
if (options.pipelineVariables) {
// Create all combinations of bitrate, resolution, and variables
Object.entries(options.pipelineVariables).forEach(([variableName, values]) => {
//console.log(`variableName: ${variableName}`);
pairs = pairs.flatMap(pair =>
values.map( value => {
const variables = pair.ffmpegOptionVariables ? {...pair.ffmpegOptionVariables} : {};
Expand Down
70 changes: 70 additions & 0 deletions src/encoreYamlGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as fs from 'fs';
import YAML from 'yaml';
import { Resolution } from '../src/models/resolution';

export class EncoreYAMLGenerator {


/**
* Creates an Encore YAML profile that contains transcoding instructions.
* @param inlineProfile YAML profile to use for transcoding.
* @param resolutions Resolutions that are to be generated.
* @param bitRates Bit rates that are to be generated for each resolution.
*/
createProfile(inlineProfile: string, resolutions: Resolution[], bitRates: number[]): string {

const inlineProfileObject = YAML.parse(inlineProfile);
let profiles: string[] = [];

resolutions.forEach(resolution => {
bitRates.forEach(bitRate => {
const encoding = this.modifyEncoreProfileAttributes(inlineProfileObject.encodes[0], resolution, bitRate);
profiles.push(encoding)
})
});

let i = 0;
profiles.forEach(encodingProfile => {
inlineProfileObject.encodes[i] = encodingProfile;
i++;
})

const profile = YAML.stringify(inlineProfileObject);
this.saveToFile("test_output_profile.yml", profile);

return profile
}

/**
* Modifies an Encore YAML profile with new values for target resolution and bitrate.
* @param profileEncodesObject The encodes attribute object of the encore YAML profile being used.
* @param resolutions Resolutions that are to be generated.
* @param bitRates Bit rates that are to be generated for each resolution.
*/
modifyEncoreProfileAttributes(profileEncodesObject: any, resolution: Resolution, bitRate: number): string {

if(!profileEncodesObject){
throw new Error('encodes object missing');
}
const data = profileEncodesObject;

data.height = resolution.height;
data.width = resolution.width;
data.params['b:v'] = bitRate;
data.params['maxrate'] = resolution.range?.max;
data.params['minrate'] = resolution.range?.min;

const updatedYAML = YAML.stringify(data);

return updatedYAML;
}

/**
* Saves an Encore YAML profile to a file. Mostly for testing.
* @param filePath The path where to write the file.
* @param yaml The yaml-structured content.
*/
saveToFile(filePath: string, yaml: string) {
fs.writeFileSync(filePath, yaml);
}
}
17 changes: 17 additions & 0 deletions src/load-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import YAML from 'yaml';
import LocalPipeline from './pipelines/local/local-pipeline';
import AWSPipeline from './pipelines/aws/aws-pipeline';
import { EncorePipeline } from './pipelines/encore/encore-pipeline';
import { Pipeline } from './pipelines/pipeline';
import logger from './logger';
import { LocalPipelineConfiguration } from './pipelines/local/local-pipeline-configuration';
Expand Down Expand Up @@ -30,6 +31,20 @@ export type PipelineProfile = {
pythonPath: string;
ffmpegEncoder: 'libx264' | 'h264_videotoolbox';
};

encore?: {
apiAddress: string;
token: string;
instanceId: string;
profilesUrl: string;
outputFolder: string;
baseName: string;
input: string;
duration: number;
priority: number;
encorePollingInterval_ms: number;
encoreInstancePostCreationDelay_ms: number
}
};

/**
Expand All @@ -53,6 +68,8 @@ async function loadPipeline(pipelineFilename: string, encodingProfile?: string):
return new AWSPipeline({ ...pipelineProfile.aws, mediaConvertSettings: encodingProfileData });
} else if (pipelineProfile.local !== undefined) {
return new LocalPipeline({ ...pipelineProfile.local, ffmpegOptions: encodingProfileData });
} else if (pipelineProfile.encore !== undefined) {
return new EncorePipeline(pipelineProfile.encore);
} else {
throw new Error(`Invalid pipeline: ${JSON.stringify(pipelineProfile)}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pipelines/encore/encore-pipeline-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ export type EncorePipelineConfiguration = {
apiAddress: string;
token: string;
instanceId: string;
profile: string;
profilesUrl: string;
outputFolder: string;
baseName: string;
inputs: Array<string>;
input: string;
duration: number;
priority: number;
encorePollingInterval_ms: number;
Expand Down
Loading

0 comments on commit 81dea1a

Please sign in to comment.