Skip to content

Commit

Permalink
Add a config allow ffmpeg to use external font from a directory (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
EverettSummer authored Jan 15, 2023
1 parent 78673fd commit 4730a82
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ yarn-error.log
test.json
start-scheduler.sh
start-executor.sh
start-e2ehelper.sh
start-e2ehelper.sh
fonts/
5 changes: 4 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ jobLogPath: '${project_root}/log/jobs'
jobExpireTime:
Canceled: 1
UnrecoverableError: 7
Finished: 2
Finished: 2

# fonts directory that can be used by ffmpeg
fontsDir: ${project_root}/fonts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mira-video-manager",
"version": "1.1.2",
"version": "1.2.0",
"description": "Video Process for mira project",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 7 additions & 3 deletions src/processors/profiles/BaseProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
import { ConvertAction } from '../../domains/ConvertAction';

export abstract class BaseProfile {
protected constructor(protected action: ConvertAction) {
protected constructor(protected action: ConvertAction, protected fontsDir: string) {
}

/**
Expand All @@ -44,7 +44,11 @@ export abstract class BaseProfile {
cmd = cmd.concat(['-i', this.action.audioFilePath]);
}
if (this.action.subtitlePath) {
cmd = cmd.concat(['-i', this.action.audioFilePath]);
if (this.fontsDir) {
cmd = cmd.concat(['-vf', `subtitles='${this.action.subtitlePath}':fontsdir='${this.fontsDir}'`]);
} else {
cmd = cmd.concat(['-vf', `subtitles='${this.action.subtitlePath}'`]);
}
}
return cmd;
}
Expand Down
6 changes: 3 additions & 3 deletions src/processors/profiles/ContainerOnlyProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,8 @@ import { ConvertAction } from '../../domains/ConvertAction';
export class ContainerOnlyProfile extends BaseProfile {
public static profileName = 'container_only';

constructor(action: ConvertAction) {
super(action);
constructor(action: ConvertAction, fontsDir: string) {
super(action, fontsDir);
}

public getCommandArgs(): Promise<string[]> {
Expand Down
20 changes: 3 additions & 17 deletions src/processors/profiles/DefaultProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,27 +18,13 @@ import { BaseProfile } from "./BaseProfile";
import { ConvertAction } from '../../domains/ConvertAction';

export class DefaultProfile extends BaseProfile {
constructor(action: ConvertAction) {
super(action);
constructor(action: ConvertAction, fontsDir: string) {
super(action, fontsDir);
}

public static profileName = 'default';

public getCommandArgs(): Promise<string[]> {
return Promise.resolve([...this.getInputCommandArgs(), '-strict', '-2']);
}

protected getInputCommandArgs(): string[] {
let cmd = [];
if (this.action.videoFilePath) {
cmd = cmd.concat(['-i', this.action.videoFilePath]);
}
if (this.action.audioFilePath) {
cmd = cmd.concat(['-i', this.action.audioFilePath]);
}
if (this.action.subtitlePath) {
cmd = cmd.concat(['-vf', `subtitles='${this.action.subtitlePath}'`]);
}
return cmd;
}
}
14 changes: 9 additions & 5 deletions src/processors/profiles/ProfileFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,19 +21,23 @@ import { interfaces } from "inversify";
import { BaseProfile } from "./BaseProfile";
import { ContainerOnlyProfile } from './ContainerOnlyProfile';
import { ConvertAction } from '../../domains/ConvertAction';
import { ConfigManager } from '../../utils/ConfigManager';
import { TYPES } from '@irohalab/mira-shared';

export function ProfileFactory(context: interfaces.Context): ProfileFactoryInitiator {
const configManager = context.container.get<ConfigManager>(TYPES.ConfigManager);
const fontsDir = configManager.fontsDir();
return (profileName: string, action: ConvertAction, profileExtraData?: any) => {
switch (profileName) {
case SoundOnlyProfile.profileName:
return new SoundOnlyProfile(action, profileExtraData.data);
return new SoundOnlyProfile(action, profileExtraData.data, fontsDir);
case VideoOnlyProfile.profileName:
return new VideoOnlyProfile(action);
return new VideoOnlyProfile(action, fontsDir);
case ContainerOnlyProfile.profileName:
return new ContainerOnlyProfile(action);
return new ContainerOnlyProfile(action, fontsDir);
case DefaultProfile.profileName:
default:
return new DefaultProfile(action);
return new DefaultProfile(action, fontsDir);
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/processors/profiles/SoundOnlyProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,8 +31,8 @@ const DEFAULT_BIT_RATE_PER_CHANNEL = 64; // kbit/s
export class SoundOnlyProfile extends BaseProfile {
public static profileName = 'sound_only'

constructor(action: ConvertAction, private _preferredTrack: string) {
super(action);
constructor(action: ConvertAction, private _preferredTrack: string, fontsDir: string) {
super(action, fontsDir);
}

private static isPropertyEqual(prop1: any, prop2: string): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/processors/profiles/VideoOnlyProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { ConvertAction } from '../../domains/ConvertAction';
import { DefaultProfile } from './DefaultProfile';

export class VideoOnlyProfile extends DefaultProfile {
constructor(action: ConvertAction) {
super(action);
constructor(action: ConvertAction, fontsDir: string) {
super(action, fontsDir);
}

public static profileName = 'video_only'
Expand Down
6 changes: 5 additions & 1 deletion src/test-helpers/FakeConfigManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -111,4 +111,8 @@ export class FakeConfigManager implements ConfigManager {
public getJobExpireTime(): { Canceled: number; UnrecoverableError: number; Finished: number } {
return {Canceled: 1, Finished: 1, UnrecoverableError: 1};
}

public fontsDir(): string {
return '';
}
}
7 changes: 6 additions & 1 deletion src/utils/ConfigManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,4 +65,9 @@ export interface ConfigManager extends BaseConfigManager {
* Unit is days
*/
getJobExpireTime(): {Canceled: number, UnrecoverableError: number, Finished: number};

/**
* Get fonts directory
*/
fontsDir(): string;
}
11 changes: 10 additions & 1 deletion src/utils/ConfigManagerImpl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IROHA LAB
* Copyright 2023 IROHA LAB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@ type AppConfg = {
};
jobLogPath: string;
jobExpireTime: {Canceled: number, UnrecoverableError: number, Finished: number};
fontsDir: string;
};

const CWD_PATTERN = /\${cwd}/;
Expand Down Expand Up @@ -239,4 +240,12 @@ export class ConfigManagerImpl implements ConfigManager {
return {Canceled: 1, UnrecoverableError: 5, Finished: 3};
}
}

public fontsDir(): string {
const fontsDirStr = this._config.fontsDir;
if (fontsDirStr) {
return ConfigManagerImpl.processPath(fontsDirStr);
}
return null;
}
}

0 comments on commit 4730a82

Please sign in to comment.