Skip to content

The swiss army knife of lossless video/audio editing

Preserve file modification time when cutting/screenshotting

Mikael Finstad60060d4b73c6
diff --git a/src/capture-frame.js b/src/capture-frame.js
index 9bc3759a..b6f4dd25 100644
--- a/src/capture-frame.js
+++ b/src/capture-frame.js
@@ -19,14 +19,15 @@ function getFrameFromVideo(video, format) {
return strongDataUri.decode(dataUri);
}
-function captureFrame(customOutDir, filePath, video, currentTime, captureFormat) {
+async function captureFrame(customOutDir, filePath, video, currentTime, captureFormat) {
const buf = getFrameFromVideo(video, captureFormat);
const ext = mime.extension(buf.mimetype);
const time = util.formatDuration(currentTime);
const outPath = util.getOutPath(customOutDir, filePath, `${time}.${ext}`);
- return fs.writeFileAsync(outPath, buf);
+ await fs.writeFileAsync(outPath, buf);
+ return util.transferTimestamps(filePath, outPath);
}
module.exports = captureFrame;
diff --git a/src/ffmpeg.js b/src/ffmpeg.js
index ffb37865..5216213f 100644
--- a/src/ffmpeg.js
+++ b/src/ffmpeg.js
@@ -55,8 +55,7 @@ function handleProgress(process, cutDuration, onProgress) {
});
}
-function cut(customOutDir, filePath, format, cutFrom, cutTo, onProgress) {
- return bluebird.try(() => {
+async function cut(customOutDir, filePath, format, cutFrom, cutTo, onProgress) {
const extWithoutDot = path.extname(filePath) || `.${format}`;
const ext = `.${extWithoutDot}`;
const duration = `${util.formatDuration(cutFrom)}-${util.formatDuration(cutTo)}`;
@@ -76,16 +75,13 @@ function cut(customOutDir, filePath, format, cutFrom, cutTo, onProgress) {
onProgress(0);
- return getFfmpegPath()
- .then((ffmpegPath) => {
+ const ffmpegPath = await getFfmpegPath();
const process = execa(ffmpegPath, ffmpegArgs);
handleProgress(process, cutTo - cutFrom, onProgress);
- return process;
- })
- .then((result) => {
+ const result = await process;
console.log(result.stdout);
- });
- });
+
+ return util.transferTimestamps(filePath, outPath);
}
/**
diff --git a/src/renderer.jsx b/src/renderer.jsx
index fe4e450b..b5093738 100644
--- a/src/renderer.jsx
+++ b/src/renderer.jsx
@@ -236,7 +236,7 @@ class App extends React.Component {
});
}
- cutClick() {
+ async cutClick() {
if (this.state.working) return alert('I\'m busy');
const cutStartTime = this.state.cutStartTime;
@@ -252,25 +252,26 @@ class App extends React.Component {
this.setState({ working: true });
const outputDir = this.state.outputDir;
const fileFormat = this.state.fileFormat;
- return ffmpeg.cut(
+ try {
+ return await ffmpeg.cut(
outputDir,
filePath,
fileFormat,
cutStartTime,
cutEndTime,
progress => this.onCutProgress(progress),
- )
- .catch((err) => {
+ );
+ } catch (err) {
console.error('stdout:', err.stdout);
console.error('stderr:', err.stderr);
if (err.code === 1 || err.code === 'ENOENT') {
- alert('Whoops! ffmpeg was unable to cut this video. It may be of an unknown format or codec combination');
- return;
+ return alert('Whoops! ffmpeg was unable to cut this video. It may be of an unknown format or codec combination');
+ }
+ return ffmpeg.showFfmpegFail(err);
+ } finally {
+ this.setState({ working: false });
}
- ffmpeg.showFfmpegFail(err);
- })
- .finally(() => this.setState({ working: false }));
}
capture() {
diff --git a/src/util.js b/src/util.js
index 8c3a614f..800accf4 100644
--- a/src/util.js
+++ b/src/util.js
@@ -1,5 +1,6 @@
const _ = require('lodash');
const path = require('path');
+const fs = require('fs');
function formatDuration(_seconds) {
const seconds = _seconds || 0;
@@ -23,7 +24,17 @@ function getOutPath(customOutDir, filePath, nameSuffix) {
`${filePath}-${nameSuffix}`;
}
+async function transferTimestamps(inPath, outPath) {
+ try {
+ const stat = await fs.statAsync(inPath);
+ await fs.utimesAsync(outPath, stat.atime.getTime() / 1000, stat.mtime.getTime() / 1000);
+ } catch (err) {
+ console.error('Failed to set output file modified time', err);
+ }
+}
+
module.exports = {
formatDuration,
getOutPath,
+ transferTimestamps,
};