【发布时间】:2019-12-07 00:05:22
【问题描述】:
主要目标:转换图像,然后按特定顺序制作转换图像的视频(使用 videoshow)。
问题:图片无序。
预期:有一个文件路径列表,就像我在 finder 中看到的那样,按文件名排序。
现实:我得到一个未排序的文件路径列表。由于我使用
videoshow生成视频,这对我来说是一个严重的问题,因为添加的图像是无序的。尝试:
我正在使用异步代码,我认为这可能是问题所在,尽管我目前使用的是
fs.readdirSync()...我尝试过使用
glob模块,但也没有用。 我曾尝试在与文件名读取相关的所有内容之前放置一个await关键字,但它也没有奏效。
代码
const state = require("./state.js");
const fs = require("fs");
const originalDir = "./content/images/";
const resizedDir = "./content/images/resized/";
async function robot() {
const content = state.load();
await acquireImages();
console.log(content.images)
async function acquireImages() {
originalFiles = fs.readdirSync(originalDir);
imagesList = [];
resizedImagesList = [];
originalFiles.forEach(file => {
if (file.slice(-3) == "png") {
imagesList.push({
filePath: originalDir + file,
fileName: file.slice(0, -8)
});
}
});
await imagesList.forEach(image => convertImage(image));
await populateImageList();
}
async function populateImageList() {
resizedFilenames = await fs.readdirSync(resizedDir);
resizedImagesList = [];
for (const file of resizedFilenames) {
if (file.slice(-3) == "png") {
resizedImagesList.push(resizedDir + file);
}
}
content.images = resizedImagesList;
}
}
module.exports = robot;
预期输出:
就像在 Finder 中一样。
电流输出:
[ '10_123.5-126.6(2)(converted).png',
'11_123.5-126.6(3)(converted).png',
'12_139.6-139.8(1)(converted).png',
'13_139.6-139.8(2)(converted).png',
'14_139.6-139.8(3)(converted).png',
'15_166.1-170.6(1)(converted).png',
'16_166.1-170.6(2)(converted).png',
'17_166.1-170.6(3)(converted).png',
'18_188.8-189.4(1)(converted).png',
'19_188.8-189.4(2)(converted).png',
'1_113.3-113.7(1)(converted).png',
'20_188.8-189.4(3)(converted).png',
'21_6.6-7(1)(converted).png',
'22_6.6-7(2)(converted).png',
'23_6.6-7(3)(converted).png',
'24_68.5-68.6(1)(converted).png',
'25_68.5-68.6(2)(converted).png',
'26_68.5-68.6(3)(converted).png',
'27_77.1-77.4(1)(converted).png',
'28_77.1-77.4(2)(converted).png',
'29_77.1-77.4(3)(converted).png',
'2_113.3-113.7(3)(converted).png',
'30_81.7-81.9(1)(converted).png',
'31_81.7-81.9(2)(converted).png',
'32_81.7-81.9(3)(converted).png',
'33_87.3-87.4(1)(converted).png',
'34_87.3-87.4(2)(converted).png',
'35_87.3-87.4(3)(converted).png',
'36_96.3-96.9(1)(converted).png',
'37_96.3-96.9(2)(converted).png',
'38_96.3-96.9(3)(converted).png',
'39_undefined-undefined(1)(converted).png',
'3_116.7-118.6(1)(converted).png',
'40_undefined-undefined(2)(converted).png',
'41_undefined-undefined(3)(converted).png',
'4_116.7-118.6(2)(converted).png',
'5_116.7-118.6(3)(converted).png',
'6_121.6-122.3(1)(converted).png',
'7_121.6-122.3(2)(converted).png',
'8_121.6-122.3(3)(converted).png',
'9_123.5-126.6(1)(converted).png' ]
【问题讨论】:
标签: javascript node.js asynchronous fs