Skip to content

Commit

Permalink
Fixed an issue when a folder was empty
Browse files Browse the repository at this point in the history
  • Loading branch information
wictorwilen committed Jan 3, 2024
1 parent 08f574f commit 670c53d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ring-timelapse-docker",
"version": "2.0.0",
"version": "2.1.0",
"description": "Ring Timelapse creator as a Docker container",
"scripts": {
"build": "tsc",
Expand Down
84 changes: 44 additions & 40 deletions src/timelapse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,57 @@ async function timelapse() {
folders.forEach(f => {
if (lstatSync(path.resolve(__dirname, "target", f)).isDirectory()) {
console.log(f);
const files = readdirSync(path.resolve(__dirname, "target", f));

let command = FfmpegCommand();
if (files.length > 0) {

let command = FfmpegCommand();


let template = "";
const templateFilePath = path.resolve(__dirname, "target", f + "-" + Date.now() + '.txt');

const files = readdirSync(path.resolve(__dirname, "target", f));
let template = "";
const templateFilePath = path.resolve(__dirname, "target", f + "-" + Date.now() + '.txt');

command.on('error', (err) => {
console.log('An error occurred: ' + err.message);
});

command.on('error', (err) => {
console.log('An error occurred: ' + err.message);
});
// Cleanup commands once timelapse is done
command.on('end', () => {
console.log('Merging finished, removing snapshot images!');
for (const file of files) {
rmSync(path.resolve(__dirname, "target", f, file));
}
rmSync(templateFilePath);
console.log("Done!");
});

// Cleanup commands once timelapse is done
command.on('end', () => {
console.log('Merging finished, removing snapshot images!');
for (const file of files) {
rmSync(path.resolve(__dirname, "target", f, file));
// add all the image files
for (const file of files.sort((a, b) => {
return lstatSync(path.resolve(__dirname, "target", f, a)).mtimeMs -
lstatSync(path.resolve(__dirname, "target", f, b)).mtimeMs;
})) {
console.log(`Adding ${file}`);
template += `file ${path.resolve(__dirname, "target", f, file)}\n`;
}
rmSync(templateFilePath);
console.log("Done!");
});

// add all the image files
for (const file of files.sort((a, b) => {
return lstatSync(path.resolve(__dirname, "target", f, a)).mtimeMs -
lstatSync(path.resolve(__dirname, "target", f, b)).mtimeMs;
})) {
console.log(`Adding ${file}`);
template += `file ${path.resolve(__dirname, "target", f, file)}\n`;
}

// add the last file on additional time
template += `file ${path.resolve(__dirname, "target", f, files[files.length - 1])}\n`;

// write the template file to disk
writeFileSync(templateFilePath, template);

// configure ffmpeg
command.fpsOutput(24);
command.addInput(templateFilePath);
command.inputOptions(["-f", "concat", "-safe", "0"])
command.videoCodec("libx264")
command.noAudio()
command.format("mp4");

// persist the file
command.save(path.resolve(__dirname, "target", f + "-" + Date.now() + '.mp4'));
// add the last file on additional time
template += `file ${path.resolve(__dirname, "target", f, files[files.length - 1])}\n`;

// write the template file to disk
writeFileSync(templateFilePath, template);

// configure ffmpeg
command.fpsOutput(24);
command.addInput(templateFilePath);
command.inputOptions(["-f", "concat", "-safe", "0"])
command.videoCodec("libx264")
command.noAudio()
command.format("mp4");

// persist the file
command.save(path.resolve(__dirname, "target", f + "-" + Date.now() + '.mp4'));
}
}
});

Expand Down

0 comments on commit 670c53d

Please sign in to comment.