Skip to content

Commit

Permalink
Allow custom output args mifi#27 mifi#36
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Dec 8, 2020
1 parent 9edb1e4 commit 91e06ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Edit specs are JavaScript / JSON objects describing the whole edit operation wit
| `width` | `--width` | Width which all media will be converted to | `640` | |
| `height` | `--height` | Height which all media will be converted to | auto based on `width` and aspect ratio of **first video** | |
| `fps` | `--fps` | FPS which all videos will be converted to | First video FPS or `25` | |
| `customOutputArgs` | | Specify custom output codec/format arguments for ffmpeg (See [example](https://github.com/mifi/editly/blob/master/examples/customOutputArgs.json5)) | auto (h264) | |
| `allowRemoteRequests` | `--allow-remote-requests` | Allow remote URLs as paths | `false` | |
| `fast` | `--fast`, `-f` | Fast mode (low resolution and FPS, useful for getting a quick preview ⏩) | `false` | |
| `defaults.layer.fontPath` | `--font-path` | Set default font to a .ttf | System font | |
Expand Down
7 changes: 7 additions & 0 deletions examples/customOutputArgs.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
outPath: './customOutputArgs.webp',
clips: [
{ duration: 2, layers: [{ type: 'title-background', text: 'Custom output args' }] },
],
customOutputArgs: ['-compression_level', '5', '-qscale', '60', '-vcodec', 'libwebp'],
}
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const Editly = async (config = {}) => {
allowRemoteRequests,
audioNorm,
outputVolume,
customOutputArgs,

ffmpegPath = 'ffmpeg',
ffprobePath = 'ffprobe',
Expand Down Expand Up @@ -177,13 +178,16 @@ const Editly = async (config = {}) => {
return runGlTransitionOnFrame({ fromFrame, toFrame, progress, transitionName, transitionParams });
}

function startFfmpegWriterProcess() {
function getOutputArgs() {
if (customOutputArgs) {
assert(Array.isArray(customOutputArgs), 'customOutputArgs must be an array of arguments');
return customOutputArgs;
}

// https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
const outputArgs = isGif ? [
'-vf',
`format=rgb24,fps=${fps},scale=${width}:${height}:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse`,
const videoOutputArgs = isGif ? [
'-vf', `format=rgb24,fps=${fps},scale=${width}:${height}:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse`,
'-loop', 0,
'-y', outPath,
] : [
'-vf', 'format=yuv420p',
'-vcodec', 'libx264',
Expand All @@ -192,9 +196,14 @@ const Editly = async (config = {}) => {
'-crf', '18',

'-movflags', 'faststart',
'-y', outPath,
];

const audioOutputArgs = audioFilePath ? ['-acodec', 'aac', '-b:a', '128k'] : [];

return [...audioOutputArgs, ...videoOutputArgs];
}

function startFfmpegWriterProcess() {
const args = [
...(enableFfmpegLog ? [] : ['-hide_banner', '-loglevel', 'error']),

Expand All @@ -210,9 +219,9 @@ const Editly = async (config = {}) => {
...(!isGif ? ['-map', '0:v:0'] : []),
...(audioFilePath ? ['-map', '1:a:0'] : []),

...(audioFilePath ? ['-acodec', 'aac', '-b:a', '128k'] : []),
...getOutputArgs(),

...outputArgs,
'-y', outPath,
];
if (verbose) console.log('ffmpeg', args.join(' '));
return execa(ffmpegPath, args, { encoding: null, buffer: false, stdin: 'pipe', stdout: process.stdout, stderr: process.stderr });
Expand Down

0 comments on commit 91e06ba

Please sign in to comment.