fluent-ffmpeg / node-fluent-ffmpeg

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

Unable to convert m4a to mp3 through streams but works with file path #616

Open anurag-gethullo opened 8 years ago

anurag-gethullo commented 8 years ago

When I run fluent-ffmpeg with file path, it gives me an proper output, but while running the same script with file stream reader it generates an mp3 which only contains ffprob _ID3 TXXX major_brandM4A TXXX minor_version512TXXX compatiblebrandsisomiso2TSSE Lavf57.41.100 My code is:

var fileReader = fs.createReadStream(__dirname + "/audio.m4a");
var tp = ffmpeg(fileReader)
    .withNoVideo()
    .inputFormat('m4a')
    .audioCodec('libmp3lame')
    .audioBitrate(128)
    .format('mp3')
    .on('error', (err) => console.error(err))
    .on('end', () => console.log('Finished!'))
    .save(fs.createWriteStream("audio.mp3"));

The wired thing for me is why the same audio.m4a is working great with file path, but not working with streams? FYI i am working on my local machine.

njoyard commented 8 years ago

Looks like a ffmpeg issue. Can you please try with command line ffmpeg first and report back if it works ?

anurag-gethullo commented 8 years ago

Hey thanks for your reply. command line ffmpeg works fine: ffmpeg -f m4a -i audio.m4a -acodec libmp3lame -f mp3 output.mp3 if I give the file path instead of streams it works as expected the file path code is:

ffmpeg(__dirname + "/audio.m4a")
     .withNoVideo()
     .inputFormat('m4a')
     .audioCodec('libmp3lame')
     .audioBitrate(128)
     .format('mp3')
     .on('error', (err) => console.error(err))
     .on('end', () => console.log('Finished!'))
     .pipe(fs.createWriteStream("streamed.mp3"));

The only problem I'am facing is with streams

anurag-gethullo commented 8 years ago

If i call the ffmpeg code on "end" event of streams in that case it throws an error throw new Error('Invalid input');

the complete stack trace of it is:

Error: Invalid input
    at FfmpegCommand.proto.mergeAdd.proto.addInput.proto.input (/Users/anuragtiwari/Code/RND/ffmpeg-fulent/node_modules/fluent-ffmpeg/lib/options/inputs.js:34:15)
    at new FfmpegCommand (/Users/anuragtiwari/Code/RND/ffmpeg-fulent/node_modules/fluent-ffmpeg/lib/fluent-ffmpeg.js:51:10)
    at FfmpegCommand (/Users/anuragtiwari/Code/RND/ffmpeg-fulent/node_modules/fluent-ffmpeg/lib/fluent-ffmpeg.js:34:12)
    at ReadStream.<anonymous> (/Users/anuragtiwari/Code/RND/ffmpeg-fulent/index.js:39:6)
    at emitNone (events.js:72:20)
    at ReadStream.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:921:12)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickCallback (node.js:356:17)
anurag-gethullo commented 8 years ago

Can you please confirm if the same code works for you with streams?

njoyard commented 8 years ago

command line ffmpeg works fine: ffmpeg -f m4a -i audio.m4a -acodec libmp3lame -f mp3 output.mp3 if I give the file path instead of streams it works as expected

You already knew it worked fine with file paths. What needs to be tested is using pipes on command line.

jimbuck commented 7 years ago

I get a similar issue (Error: Invalid input at FfmpegCommand.proto.mergeAdd.proto.addInput) and it is caused by my input stream being a through2 stream (progress-stream actually). Since it is a custom transform stream it does not have the "readable" flag set.

thiagobustamante commented 7 years ago

I am having the same problem. It works with file paths and does not work with streams

freitasjuciel commented 5 years ago

Hi, anyone has a solution for it ?

mikestonecodes commented 5 years ago

I just got this bug also

mitchell-bu commented 5 years ago

I'm also experiencing this issue. I can convert when the m4a is a file, but piping it from a stream claims 'partial file'. This SO question says that "ffmpeg in streaming mode needs that header information to be near the beginning of the file. Fixing the file using ffmpeg -acodec copy -movflags faststart works for me." https://stackoverflow.com/questions/52543595/problem-using-ffmpeg-in-streaming-mode-on-aac-files-created-by-android

PaddyOl commented 2 years ago

Hi, I still can't make it work... even after following @mitchell-bu proposal (on SO) What i want to do is convert from m4a to wav.

let chunks = [];

Ffmpeg(inputStream(content)) .audioCodec('pcm_s16le') .audioChannels(1) .format('wav') .outputOptions('-movflags faststart'); //-acodec copy <= this does not seem to be supported (?) .on('end', () => { console.info(finished.); }) .on('error', (err) => { console.error(error is ${err}); });

            const ffstream = command.pipe({end: true});
            ffstream.on('data', function (chunk) {
                chunks.push(chunk);
            });

I just added the outputOptions line to solve the m4a to wav conversion problem.

Note that command line ffmpeg -i myFile.m4a outFile.wav works fine .... Note also that the above code works fine for other audio format to wav conversion (removing the outputOptions line)

Where am i getting wrong ? Thx.

ghost commented 1 year ago

Any updates on this? We're experiencing the same at my company and we are unable to fix it. CMD works fine.

simhskal commented 1 year ago

Has there been an update on this issue? I am currently facing an issue and unable to find a workaround

MegaTeam89 commented 1 year ago

I am also unable to find a workaround, I can't tell if this is a bug with ffmpeg or fluent ffmpeg, has anybody tried filing a report with ffmpeg regarding this?

hypo-thesis commented 1 year ago

Just got this bug and realized this has existed since 7 years ago.

MegaTeam89 commented 1 year ago

I find even more crazy how nobody has found a solution, or even a workaround yet

dannycortesv commented 1 year ago

I also need a solution for this problem, anyone has find a workaround?

alireza-hariri commented 1 year ago

this is not a problem with fluent-ffmpeg as mitchell-bu said. the headers of m4a are at the end of file so stream can have problem.

adding .inputOptions('-codec:a' ,'libfdk_aac') may solve this