The swiss army knife of lossless video/audio editing
merge transferTimestamps and transferTimestampsWithOffset
Mikael Finstad62f99a5e7e15
diff --git a/src/capture-frame.js b/src/capture-frame.jsindex f6edff7f..67d656d0 100644--- a/src/capture-frame.js+++ b/src/capture-frame.js@@ -1,6 +1,6 @@import strongDataUri from 'strong-data-uri';-import { formatDuration, getOutPath, transferTimestampsWithOffset } from './util';+import { formatDuration, getOutPath, transferTimestamps } from './util';import { captureFrame as ffmpegCaptureFrame } from './ffmpeg';@@ -19,9 +19,9 @@ function getFrameFromVideo(video, format) {return strongDataUri.decode(dataUri);}-async function transferTimestamps({ duration, currentTime, fromPath, toPath }) {+async function transferTimestampsWithTime({ duration, currentTime, fromPath, toPath }) {const offset = -duration + currentTime;- await transferTimestampsWithOffset(fromPath, toPath, offset);+ await transferTimestamps(fromPath, toPath, offset);}export async function captureFrameFfmpeg({ customOutDir, filePath, currentTime, captureFormat, duration }) {@@ -29,7 +29,7 @@ export async function captureFrameFfmpeg({ customOutDir, filePath, currentTime,const outPath = getOutPath(customOutDir, filePath, `${time}.${captureFormat}`);await ffmpegCaptureFrame({ timestamp: currentTime, videoPath: filePath, outPath });- await transferTimestamps({ duration, currentTime, fromPath: filePath, toPath: outPath });+ await transferTimestampsWithTime({ duration, currentTime, fromPath: filePath, toPath: outPath });return outPath;}@@ -42,6 +42,6 @@ export async function captureFrameFromTag({ customOutDir, filePath, currentTime,const outPath = getOutPath(customOutDir, filePath, `${time}.${ext}`);await fs.writeFile(outPath, buf);- await transferTimestamps({ duration, currentTime, fromPath: filePath, toPath: outPath });+ await transferTimestampsWithTime({ duration, currentTime, fromPath: filePath, toPath: outPath });return outPath;}diff --git a/src/util.js b/src/util.jsindex 9576a938..e5be0334 100644--- a/src/util.js+++ b/src/util.js@@ -89,20 +89,10 @@ export async function dirExists(dirPath) {return (await fs.exists(dirPath)) && (await fs.lstat(dirPath)).isDirectory();}-export async function transferTimestamps(inPath, outPath) {+export async function transferTimestamps(inPath, outPath, offset = 0) {try {- const stat = await fs.stat(inPath);- await fs.utimes(outPath, stat.atime.getTime() / 1000, stat.mtime.getTime() / 1000);- } catch (err) {- console.error('Failed to set output file modified time', err);- }-}--export async function transferTimestampsWithOffset(inPath, outPath, offset) {- try {- const stat = await fs.stat(inPath);- const time = (stat.mtime.getTime() / 1000) + offset;- await fs.utimes(outPath, time, time);+ const { atime, mtime } = await fs.stat(inPath);+ await fs.utimes(outPath, (atime.getTime() / 1000) + offset, (mtime.getTime() / 1000) + offset);} catch (err) {console.error('Failed to set output file modified time', err);}