How to join ogg files?

How could I run the equivalent in nodejs the sgte command in bash.

ls | sort -V |xargs cat output.ogg

What I want is to sort a series of ogg files and merge them into output.ogg.

Update

Use the code you left:

var fs = require("fs");
var path="....................RUTA............";


function ordenar(a,b){
    var n=parseInt(a.substr(6,a.indexOf(".ogg")-6));
    var m=parseInt(b.substr(6,b.indexOf(".ogg")-6));
    if(n>m){
    return 1;
    }
    if(n<m){
    return -1;
    }
    else{
    return 0;
    }
}

function dumpData(dirPath, outputFile) {
    // Leer directorio
    fs.readdir(dirPath,function(err, files) {
        if (err) throw err;
        files.sort(ordenar);
        files.forEach(function (file){
            // Filtrar por *.ogg
            if(/^.*\.(ogg)$/.test(file)) {
                // Leer y volcar datos al archivo de salida
                 data = fs.readFileSync(dirPath+"/"+file, 'utf8');
                fs.appendFileSync(outputFile, data);
            }
        });   
    });
}



dumpData(path+'/archivos/principito', 'output.ogg');

When you want to run it with mplayer I get the following

Playing output.ogg.
Detected file format: MPEG-PES
MPEG: FATAL: EOF while searching for sequence header.
Video: Cannot read properties.
Load subtitles in .
mpg123 init error: Error reading the stream. (code 18)
Audio decoder init failed for codecs.conf entry "mpg123".
ad_ffmpeg: initial decode failed
Audio decoder init failed for codecs.conf entry "ffmp2float".
ad_ffmpeg: initial decode failed
Audio decoder init failed for codecs.conf entry "ffmp2".
Cannot sync MAD frame
Audio decoder init failed for codecs.conf entry "mad".
Cannot sync MPA frame: 0
Audio decoder init failed for codecs.conf entry "hwmpa".
No Libav codec ID known. Generic lavc decoder is not applicable.
Audio decoder init failed for codecs.conf entry "lavc".
Cannot find codec for audio format 0x50.
Audio: no sound
Video: no video


Exiting... (End of file)
 0
Author: Kevin AB, 2016-06-03

1 answers

Node.js has fs to interact with the file system. The idea is very similar to the command in bash you use:

  1. display files (filtering by *.ogg)
  2. read each file
  3. dump data to output file

Here a possible implementation:

var fs = require("fs");

function dumpData(dirPath, outputFile) {
    // Leer directorio
    fs.readdir(dirPath,function(err, files) {
        if (err) throw err;
        files.forEach(function (file){
            // Filtrar por *.ogg
            if(/^.*\.(ogg)$/.test(file)) {
                // Leer y volcar datos al archivo de salida
                data = fs.readFileSync(file, 'utf8');
                fs.appendFileSync(outputFile, data);
            }
        });   
    });
}

dumpData('directory/my_oggfiles', 'output.ogg')

It is necessary to take into account, when it is appropriate to work asynchronously or synchronously , but that would already be another topic to try.

For more information you can consult the official documentation on fs: https://nodejs.org/api/fs.html

Greetings.

I edit: using stream to keep the format

The above code works, with plain text. In this case, with * files.ogg you need to use stream . Streams are equivalent to pipes in * nix, allowing you to read data from a source and connect it to a destination. This would be a possible solution:

var fs = require('fs');

function streamFile(files, i, outputFile) {
    if (i == files.length) {
        outputFile.end();
        return;
    }

    stream = fs.createReadStream(files[i++]);
    stream.pipe(outputFile, {end: false});

    stream.on('end', function() {
        streamFile(files, i, outputFile);
    });
}

function appendFiles(dirPath, outputFile) {
    // Leer directorio y filtrar *.ogg
    files = fs.readdirSync(dirPath).filter(function(item) {
        return /^.*\.(ogg)$/.test(item);
    });
    output = fs.createWriteStream('output.ogg');

    // Unir stream
    streamFile(files, 0, output);
}

appendFiles('my_directory/files', 'output.ogg')

Greetings.

 1
Author: Madh, 2016-06-03 23:10:43