fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.85k stars 874 forks source link

Error when transcoding audio for the second time. #609

Open renatodossantosleal opened 7 years ago

renatodossantosleal commented 7 years ago

So I have a code in which I get an audio from a URL as .aac or .mp3 and I'd like to convert it to .wav in order to do Text to Speech. I've been able to make it work the first time I say something but then when I try to say some other thing for the second time it just won't work. Is there any problem with my code?

My code right now is the following:

var watson = require('watson-developer-cloud');
var request = require('request');
var fs = require('fs');
var https = require('https');
var tmp = require('tmp');
var ffmpeg = require('fluent-ffmpeg');
var config = require('../config/config');

var STT = {};
var speech_to_text = watson.speech_to_text({
    version: 'v1',
    username: config.speechtotext.username,
    password: config.speechtotext.password
});

STT.text = function (url, cb) {
    tmp.file({postfix: '.aac'}, function _tempFileCreated (err, input, fd) {
        if (err) throw err;

        tmp.file({postfix: '.wav'}, function _tempFileCreated (err, output, fd) {
            if (err) throw err;

            save_to_file(url, input, function () {
                convert_aac_to_wav(input, output, function () {
                    convert_speech_to_text(output, cb);
                })
            })
        })
    })
}

var convert_aac_to_wav = function(input,output,cb){
    new ffmpeg(input)
        .format('wav')
        .on('start', function(commandLine) {
            console.log('Spawned Ffmpeg with command: ' + commandLine); 
        })
        .on('end', function() {
            console.log('file has been converted succesfully');
            cb();
        })
        .on('stderr', function(stderrLine) {
            console.log('Stderr output: ' + stderrLine);
        })
        .on('error', function(err) {
            console.log('an error happened: ' + err.message);
        })
        .save(output);
}

var convert_speech_to_text = function (audio, cb) {
    var _audio = fs.createReadStream(audio);
    var params = {
        audio: _audio,
        content_type: 'audio/wav',
        model: "pt-BR_NarrowbandModel"
    }

    speech_to_text.recognize(params, function (err, res) {
        if (err) {
            console.log("ERR > " + err);
            return ;
        }

        var result = res.results[res.result_index];
        var question = '';

        if (result) {
            question = result.alternatives[0].transcript;
        }

        cb(null,question);
    })
}

var save_to_file = function (url, path, cb) {
    https.get(url, function (res) {
        var output = fs.createWriteStream(path);
        res.pipe(output);

        res.on('end', function () {
            console.log("END save_to_file");
            cb();
        });

    })
}

module.exports = STT;
`
renatodossantosleal commented 7 years ago

**And these are the logs for both runs:

Log of the one that works**

Spawned Ffmpeg with command: ffmpeg -i /home/vcap/tmp/tmp-44vUjBkRUuTX0N.aac -y -f wav /home/vcap/tmp/tmp-44tcoarqU4uiVO.wav Stderr output: ffmpeg version git-2013-06-02-5711e4f Copyright (c) 2000-2013 the FFmpeg developers Stderr output: built on Jun 2 2013 07:38:40 with gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) Stderr output: libavformat 55. 8.102 / 55. 8.102 Stderr output: libavutil 52. 34.100 / 52. 34.100 Stderr output: libswscale 2. 3.100 / 2. 3.100 Stderr output: libswresample 0. 17.102 / 0. 17.102 Stderr output: configuration: --enable-shared --disable-asm --prefix=/app/vendor/ffmpeg Stderr output: libavdevice 55. 2.100 / 55. 2.100 Stderr output: libavfilter 3. 74.101 / 3. 74.101 Stderr output: [aac @ 0x26336c0] Estimating duration from bitrate, this may be inaccurate Stderr output: Duration: 00:00:04.06, bitrate: 14 kb/s Stderr output: ISFT : Lavf55.8.102 Stderr output: Stream mapping: F > aacD > 00:00:04.06 AND Input is aac audio with aac,8000 Hz,mono,fltp,14 kb/s details Stderr output: size= 52kB time=00:00:03.32 bitrate= 128.2kbits/s file has been converted succesfully Stderr output: Stderr output: Output #0, wav, to '/home/vcap/tmp/tmp-44tcoarqU4uiVO.wav': Stderr output: video:0kB audio:52kB subtitle:0 global headers:0kB muxing overhead 0.150240% Stderr output: Press [q] to stop, [?] for help Stderr output: Metadata: Stderr output: Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s Processing: 81.79791824298071% done

The second one does not work:

Spawned Ffmpeg with command: ffmpeg -i /home/vcap/tmp/tmp-44nIjsAGWlCbmW.aac -y -f wav /home/vcap/tmp/tmp-44tfO2ZODQEdNT.wav Stderr output: ffmpeg version git-2013-06-02-5711e4f Copyright (c) 2000-2013 the FFmpeg developers Stderr output: built on Jun 2 2013 07:38:40 with gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) Stderr output: configuration: --enable-shared --disable-asm --prefix=/app/vendor/ffmpeg Stderr output: libavutil 52. 34.100 / 52. 34.100 Stderr output: libavformat 55. 8.102 / 55. 8.102 Stderr output: libavfilter 3. 74.101 / 3. 74.101 Stderr output: libswresample 0. 17.102 / 0. 17.102 Stderr output: libavcodec 55. 13.100 / 55. 13.100 Stderr output: [aac @ 0x81d6c0] Format aac detected only with low score of 1, misdetection possible! Stderr output: libavdevice 55. 2.100 / 55. 2.100 Stderr output: [aac @ 0x81d6c0] Could not find codec parameters for stream 0 (Audio: aac, 0 channels, fltp): unspecified sample rate Stderr output: /home/vcap/tmp/tmp-44nIjsAGWlCbmW.aac: could not find codec parameters Stderr output: libswscale 2. 3.100 / 2. 3.100 Stderr output: built on Jun 2 2013 07:38:40 with gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) /home/vcap/tmp/tmp-44nIjsAGWlCbmW.aac: could not find codec parameters Stderr output: Consider increasing the value for the 'analyzeduration' and 'probesize' options configuration: --enable-shared --disable-asm --prefix=/app/vendor/ffmpeg libavformat 55. 8.102 / 55. 8.102 an error happened: ffmpeg exited with code 1: Consider increasing the value for the 'analyzeduration' and 'probesize' options libavfilter 3. 74.101 / 3. 74.101 libavdevice 55. 2.100 / 55. 2.100 libswresample 0. 17.102 / 0. 17.102 libswscale 2. 3.100 / 2. 3.100 [aac @ 0x81d6c0] Could not find codec parameters for stream 0 (Audio: aac, 0 channels, fltp): unspecified sample rate libavutil 52. 34.100 / 52. 34.100 ffmpeg version git-2013-06-02-5711e4f Copyright (c) 2000-2013 the FFmpeg developers libavcodec 55. 13.100 / 55. 13.100 [aac @ 0x81d6c0] Format aac detected only with low score of 1, misdetection possible! Consider increasing the value for the 'analyzeduration' and 'probesize' options /home/vcap/tmp/tmp-44nIjsAGWlCbmW.aac: could not find codec parameters