Also save to custom output dir when capturing frame

(Make common method)
pull/39/head
Mikael Finstad 9 years ago
parent 0090bf8573
commit f70c994b9f

@ -3,6 +3,8 @@ const fs = require('fs');
const mime = require('mime-types');
const strongDataUri = require('strong-data-uri');
const util = require('./util');
bluebird.promisifyAll(fs);
function getFrameFromVideo(video) {
@ -19,10 +21,13 @@ function getFrameFromVideo(video) {
return strongDataUri.decode(dataUri);
}
function captureFrame(video, outPathWithoutExt) {
function captureFrame(customOutDir, filePath, video, currentTime) {
const buf = getFrameFromVideo(video);
const ext = mime.extension(buf.mimetype);
const outPath = `${outPathWithoutExt}.${ext}`;
const time = util.formatDuration(currentTime);
const outPath = util.getOutPath(customOutDir, filePath, `${time}.${ext}`);
return fs.writeFileAsync(outPath, buf);
}

@ -35,14 +35,13 @@ function getFfmpegPath() {
});
}
function cut(outputDir, filePath, format, cutFrom, cutTo) {
function cut(customOutDir, filePath, format, cutFrom, cutTo) {
return bluebird.try(() => {
const ext = path.extname(filePath) || `.${format}`;
const extWithoutDot = path.extname(filePath) || `.${format}`;
const ext = `.${extWithoutDot}`;
const duration = `${util.formatDuration(cutFrom)}-${util.formatDuration(cutTo)}`;
const basename = path.basename(filePath);
const outFile = outputDir ?
path.join(outputDir, `${basename}-${duration}${ext}`) :
`${filePath}-${duration}${ext}`;
const outPath = util.getOutPath(customOutDir, filePath, `${duration}${ext}`);
console.log('Cutting from', cutFrom, 'to', cutTo);
@ -50,7 +49,7 @@ function cut(outputDir, filePath, format, cutFrom, cutTo) {
'-i', filePath, '-y', '-vcodec', 'copy', '-acodec', 'copy',
'-ss', cutFrom, '-t', cutTo - cutFrom,
'-f', format,
outFile,
outPath,
];
console.log('ffmpeg', ffmpegArgs.join(' '));

@ -257,9 +257,11 @@ class App extends React.Component {
}
capture() {
if (!this.state.filePath) return;
const outPathWithoutExt = `${this.state.filePath}-${util.formatDuration(this.state.currentTime)}`;
captureFrame(getVideo(), outPathWithoutExt).catch(err => alert(err));
const filePath = this.state.filePath;
const outputDir = this.state.outputDir;
const currentTime = this.state.currentTime;
if (!filePath) return;
captureFrame(outputDir, filePath, getVideo(), currentTime).catch(err => alert(err));
}
toggleHelp() {

@ -1,4 +1,5 @@
const _ = require('lodash');
const path = require('path');
function formatDuration(_seconds) {
const seconds = _seconds || 0;
@ -14,6 +15,15 @@ function formatDuration(_seconds) {
return `${hoursPadded}.${minutesPadded}.${secondsPadded}.${msPadded}`;
}
function getOutPath(customOutDir, filePath, nameSuffix) {
const basename = path.basename(filePath);
return customOutDir ?
path.join(customOutDir, `${basename}-${nameSuffix}`) :
`${filePath}-${nameSuffix}`;
}
module.exports = {
formatDuration,
getOutPath,
};

Loading…
Cancel
Save