Skip to content

Commit

Permalink
implement allowRemoteRequests #60 #61
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Sep 23, 2020
1 parent a0281c7 commit ad7a89b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Edit specs are JavaScript / JSON objects describing the whole edit operation wit
audioFilePath,
loopAudio: false,
keepSourceAudio: false,
allowRemoteRequests: false,
clips: [
{
transition,
Expand Down Expand Up @@ -156,8 +157,9 @@ Edit specs are JavaScript / JSON objects describing the whole edit operation wit
| `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` | |
| `audioFilePath` | `--audio-file-path` | Set an audio track for the whole video | | |
| `loopAudio` | `--loop-audio` | Loop the audio track if it is shorter than video? | | |
| `keepSourceAudio` | `--keep-source-audio` | Keep audio from source files | | |
| `loopAudio` | `--loop-audio` | Loop the audio track if it is shorter than video? | `false` | |
| `keepSourceAudio` | `--keep-source-audio` | Keep audio from source files | `false` | |
| `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 | |
| `defaults.layer.*` | | Set any layer parameter that all layers will inherit | | |
Expand Down
5 changes: 4 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const cli = meow(`
--audio-file-path Add an audio track
--loop-audio Loop the audio track if it is shorter than video?
--keep-source-audio Keep audio from source files
--allow-remote-requests
--fast, -f Fast mode (low resolution and FPS, useful for getting a quick preview)
--verbose, -v
Expand All @@ -48,6 +49,7 @@ const cli = meow(`
flags: {
verbose: { type: 'boolean', alias: 'v' },
keepSourceAudio: { type: 'boolean' },
allowRemoteRequests: { type: 'boolean' },
fast: { type: 'boolean', alias: 'f' },
transitionDuration: { type: 'number' },
clipDuration: { type: 'number' },
Expand Down Expand Up @@ -96,7 +98,7 @@ const cli = meow(`
params.clips = clips.map((clip) => ({ layers: [clip] }));
}

const { verbose, transitionName, transitionDuration, clipDuration, width, height, fps, audioFilePath, fontPath, fast, out: outPath, keepSourceAudio, loopAudio } = cli.flags;
const { verbose, transitionName, transitionDuration, clipDuration, width, height, fps, audioFilePath, fontPath, fast, out: outPath, keepSourceAudio, loopAudio, allowRemoteRequests } = cli.flags;

if (transitionName || transitionDuration != null) {
params.defaults.transition = {};
Expand All @@ -116,6 +118,7 @@ const cli = meow(`
if (audioFilePath) params.audioFilePath = audioFilePath;
if (loopAudio) params.loopAudio = loopAudio;
if (keepSourceAudio) params.keepSourceAudio = true;
if (allowRemoteRequests) params.allowRemoteRequests = true;
if (width) params.width = width;
if (height) params.height = height;
if (fps) params.fps = fps;
Expand Down
9 changes: 9 additions & 0 deletions examples/remote.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
outPath: './remote.mp4',
allowRemoteRequests: true,
clips: [
{ layers: [{ type: 'image', path: 'https://picsum.photos/400/400' }] },
{ layers: [{ type: 'image', path: 'https://picsum.photos/200/400' }] },
{ layers: [{ type: 'image', path: 'https://picsum.photos/400/200' }] },
],
}
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const JSON5 = require('json5');
const fs = require('fs-extra');
const { nanoid } = require('nanoid');

const { parseFps, readVideoFileInfo, readAudioFileInfo, multipleOf2 } = require('./util');
const { parseFps, readVideoFileInfo, readAudioFileInfo, multipleOf2, isUrl } = require('./util');
const { registerFont } = require('./sources/fabric');
const { createFrameSource } = require('./sources/frameSource');
const { calcTransition } = require('./transitions');
Expand All @@ -21,8 +21,6 @@ const loadedFonts = [];
// See #16
const checkTransition = (transition) => assert(transition == null || typeof transition === 'object', 'Transition must be an object');

const assertFileExists = async (path) => assert(await fs.exists(path), `File does not exist ${path}`);

module.exports = async (config = {}) => {
const {
// Testing options:
Expand All @@ -39,17 +37,26 @@ module.exports = async (config = {}) => {
audioFilePath: audioFilePathIn,
loopAudio,
keepSourceAudio,
allowRemoteRequests,

ffmpegPath = 'ffmpeg',
ffprobePath = 'ffprobe',
} = config;

const assertFileValid = async (path) => {
if (isUrl(path)) {
assert(allowRemoteRequests, 'Remote requests are not allowed');
return;
}
assert(await fs.exists(path), `File does not exist ${path}`);
};

const isGif = outPath.toLowerCase().endsWith('.gif');

let audioFilePath;
if (!isGif) audioFilePath = audioFilePathIn;

if (audioFilePath) await assertFileExists(audioFilePath);
if (audioFilePath) await assertFileValid(audioFilePath);

checkTransition(defaultsIn.transition);

Expand All @@ -73,9 +80,9 @@ module.exports = async (config = {}) => {

// https://github.com/mifi/editly/issues/39
if (['image', 'image-overlay'].includes(type)) {
await assertFileExists(restLayer.path);
await assertFileValid(restLayer.path);
} else if (type === 'gl') {
await assertFileExists(restLayer.fragmentPath);
await assertFileValid(restLayer.fragmentPath);
}

if (['fabric', 'canvas'].includes(type)) assert(typeof layer.func === 'function', '"func" must be a function');
Expand Down
4 changes: 2 additions & 2 deletions sources/fabric/fabricFrameSources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const fileUrl = require('file-url');

const { getRandomGradient, getRandomColors } = require('../../colors');
const { easeOutExpo, easeInOutCubic } = require('../../transitions');
const { getPositionProps, getFrameByKeyFrames } = require('../../util');
const { getPositionProps, getFrameByKeyFrames, isUrl } = require('../../util');

// http://fabricjs.com/kitchensink


const defaultFontFamily = 'sans-serif';

const loadImage = async (path) => new Promise((resolve) => fabric.util.loadImage(fileUrl(path), resolve));
const loadImage = async (pathOrUrl) => new Promise((resolve) => fabric.util.loadImage(isUrl(pathOrUrl) ? pathOrUrl : fileUrl(pathOrUrl), resolve));

function getZoomParams({ progress, zoomDirection, zoomAmount }) {
let scaleFactor = 1;
Expand Down
3 changes: 3 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ function getFrameByKeyFrames(keyframes, progress) {
return Object.fromEntries(Object.entries(prevKeyframe.props).map(([propName, prevVal]) => ([propName, prevVal + ((nextKeyframe.props[propName] - prevVal) * interProgress)])));
}

const isUrl = (path) => /^https?:\/\//.test(path);


module.exports = {
parseFps,
Expand All @@ -161,4 +163,5 @@ module.exports = {
readFileStreams,
getPositionProps,
getFrameByKeyFrames,
isUrl,
};

0 comments on commit ad7a89b

Please sign in to comment.