Skip to content

Commit

Permalink
Ensure FS based async operations are run sequentially, not concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
umaar committed Jan 23, 2020
1 parent ef126e1 commit 84f17ac
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ You will then end up with a folder consisting of ~1 second videos: `0001.mp4 000

# todo

- Ensure `dist`/`video-everyday/segments`/etc. are all created on project initialisation
- allow selecting multiple videos/images for a given day (shift + click?) and have them either condensed into a 1-2 second timeframe, or just allow each of them to occupy the usual time amount
- try `video-segment-duration-seconds` of 1sec and make sure things work
- ensure working state is clean, and everything is committed, handle config files
Expand Down
6 changes: 2 additions & 4 deletions src/server/lib/generate-video-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {exec as execOld} from 'child_process';
import path from 'path';
import config from 'config';
import mkdirp from 'mkdirp';
import execa from 'execa';

const exec = promisify(execOld);
const videoSegmentFolder = config.get('video-segment-folder');
Expand All @@ -28,10 +27,9 @@ async function init({mediaFile, totalVideoDuration}) {

const hrtime = process.hrtime()[1];

console.time(`MP4Box Video Segment Creation ${hrtime}`)
console.time(`MP4Box Video Segment Creation ${hrtime}`);
const {stderr} = await exec(command);
// const {stderr} = await execa(command);
console.timeEnd(`MP4Box Video Segment Creation ${hrtime}`)
console.timeEnd(`MP4Box Video Segment Creation ${hrtime}`);

const indexOfFileName = stderr.indexOf(parsedMediaFileName.name);
const indexOfFileExtension = stderr.indexOf(`${parsedMediaFileName.ext} - duration`);
Expand Down
2 changes: 1 addition & 1 deletion src/server/lib/is-valid-media-type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const allowedMediaTypes = {
video: ['.mp4', '.mov'],
video: ['.mp4', '.mov']
// Uncomment line below, currently testing issues with video only
// image: ['.jpg', '.jpeg']
};
Expand Down
8 changes: 3 additions & 5 deletions src/server/lib/prepare-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ async function init() {

let unprocessableMediaCount = 0;

// const mediaFilesWhichNeedProcessingPromises = mediaFilesWhichNeedProcessing.map(async mediaFile => {
for (const mediaFile of mediaFilesWhichNeedProcessing) {
const isVideo = getMediaType(mediaFile) === 'video';
let metadata;

try {
metadata = await getMediaMetadata(path.join(mediaFolder, mediaFile));
metadata = await getMediaMetadata(path.join(mediaFolder, mediaFile)); // eslint-disable-line no-await-in-loop
} catch (error) {
console.log('Error getting metadata', error);
}
Expand All @@ -97,7 +96,7 @@ async function init() {
if (isVideo) {
DBRecord.videoDuration = metadata.duration;

const {segmentDuration, relativeVideoSegmentPath} = await generateVideoSegment({
const {segmentDuration, relativeVideoSegmentPath} = await generateVideoSegment({ // eslint-disable-line no-await-in-loop
mediaFile,
totalVideoDuration: DBRecord.videoDuration
});
Expand All @@ -107,10 +106,9 @@ async function init() {
DBRecord.defaultVideoSegmentDuration = segmentDuration;
}

await mediaMetadataQueries.insert(DBRecord);
await mediaMetadataQueries.insert(DBRecord); // eslint-disable-line no-await-in-loop
}

// await Promise.all(mediaFilesWhichNeedProcessingPromises);
console.log(`${unprocessableMediaCount} media items couldn't be processed`);

await generateThumbnails();
Expand Down
8 changes: 3 additions & 5 deletions src/server/lib/thumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async function init() {
console.log('No video thumbails need generating');
}

const thumbnailsPromises = videoThumbnailsWhichNeedGenerating.map(async item => {
for (const item of videoThumbnailsWhichNeedGenerating) {
const {absoluteFilePathForMedia, thumbnailFolderForMedia} = item;

if (fs.existsSync(thumbnailFolderForMedia)) {
Expand All @@ -113,13 +113,11 @@ async function init() {

mkdirp.sync(thumbnailFolderForMedia);

await generateThumbnails({
await generateThumbnails({ // eslint-disable-line no-await-in-loop
absoluteFilePathForMedia,
thumbnailFolderForMedia
});
});

await Promise.all(thumbnailsPromises);
}
}

export default init;
8 changes: 3 additions & 5 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,17 @@ router.post('/consolidate-media', async (request, response) => {

rimraf.sync(`${consolidatedMediaFolder}/*`);

const selectedMediaItemsPromises = selectedMediaItems.map(async (currentMediaItem, index) => {
for (const [index, currentMediaItem] of selectedMediaItems.entries()) {
const mediaItemPath = path.join(videoSegmentFolder, currentMediaItem);
const extension = path.parse(mediaItemPath).ext;
const newFileName = (index + 1).toString().padStart(4, '0') + extension;
const terminalCommand = `cp '${mediaItemPath}' '${path.join(consolidatedMediaFolder, newFileName)}'`;
console.log(terminalCommand);
const {stderr} = await exec(terminalCommand);
const {stderr} = await exec(terminalCommand); // eslint-disable-line no-await-in-loop
if (stderr) {
console.log('stderr', stderr);
}
});

await Promise.all(selectedMediaItemsPromises);
}

console.timeEnd('Consolidate Media');

Expand Down

0 comments on commit 84f17ac

Please sign in to comment.