【问题标题】:Looping my javascript logic over files within a folder在文件夹中的文件上循环我的 javascript 逻辑
【发布时间】:2020-02-23 07:25:00
【问题描述】:

如何在文件夹中的文件上循环此脚本?这样我的 curl 函数就可以通过自动化的方法将所有的成绩单和音频文件上传到这个网站。 (低质量的温柔是一个网站,它允许它的用户从成绩单中提取 json 对象,甚至可以让我们通过音频找到句子的开始和结束时间。)

 const getAligned = require('./getAudioTranscript.js');  // require getAudioTranscript.js, file ext not required, but need ./ to indicate it is a local file
    getAligned.writeFile('_rJOSDgDG0A.m4a', '_rJOSDgDG0A.txt');

    module.exports = {
            //var request = curl.request(default_options);
            writeFile: (audioFilePath, textFileName) => {
                const curl = require('curlrequest');
                let options = {
                    method: 'POST',
                    form: [
                        "audio=@" + audioFilePath,
                        "transcript=@" + textFileName
                    ],
                    url: 'http://localhost:32768/transcriptions?',
                };
                let startDate = new Date().getTime();
                console.log(startDate);
                curl.request(options, function (err, data, meta) {
                    //res.send({ alignment: data });
                    console.log(options);
                    //del.sync([textFileName]);
                    let endDate = new Date().getTime();
                    console.log((endDate - startDate) / 1000);
            });
        },
      };

【问题讨论】:

    标签: javascript node.js loops curl module.exports


    【解决方案1】:

    首先,您必须获取目录中的所有文件。这可以在 node.js 中使用fs.readdir(path, callback) 完成。回调接受一个有错误的函数和一个包含所有文件名的文件数组。

    const fs = require('fs');
    const path = require('path');
    
    fs.readdir('dir_path', (err, files) => {
        //check error
    
        for (const file of files) {
            //check if it is a .m4a file, so that every pair gets only send once
            if (path.extname(file) === '.m4a') {
                const txt_file= path.basename(file, '.m4a') + '.txt';
                getAligned.writeFile(path.join('dir_path', file), path.join('dir_path', txt_file));
            }
        }
    });
    

    更多信息在node.js documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多