Hello, i have this code, i want to show the percentage of the conversion
`
var infs = fs.createReadStream(path);
var self = this;
var command = ffmpeg(infs);
command.on('end', function() {
return cb(null);
});
command.on('error', function() {
return cb(err);
});
command.on('progress', function(info) {
console.log('progress ' + info.percent + '%');
});
command.save(dest);`
and it show undefined in the console: 'progress undefined%'
You cannot get the progress percent when using a stream as an input.
but that was taken from the example folder in the ffmpeg repo, can you give an example on how to do it please
Replace
var infs = fs.createReadStream(path);
var self = this;
var command = ffmpeg(infs);
with
var command = ffmpeg(path);
I too get progress undefined% when doing this:
var ffmpeg = require('fluent-ffmpeg')
var proc = ffmpeg('C:\\Users\\ErraticFox\\Desktop\\me.mp4')
.on('progress', function(info) {
console.log('progress ' + info.percent + '%');
})
.on('end', function() {
console.log('file has been converted succesfully');
})
.on('error', function(err) {
console.log('an error happened: ' + err.message);
})
.save('C:\\Users\\ErraticFox\\Desktop\\output.mp3');
What am I doing wrong?
Can you please show ffmpeg output (use the 'stderr' event) ?
Stderr output: ffmpeg version N-80129-ga1953d4 Copyright (c) 2000-2016 the FFmpeg developers
Stderr output: built with gcc 5.3.0 (GCC)
Stderr output: configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmfx --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
Stderr output: libavutil 55. 24.100 / 55. 24.100
Stderr output: libavcodec 57. 43.100 / 57. 43.100
Stderr output: libavformat 57. 37.101 / 57. 37.101
Stderr output: libavdevice 57. 0.101 / 57. 0.101
Stderr output: libavfilter 6. 46.100 / 6. 46.100
Stderr output: libswscale 4. 1.100 / 4. 1.100
Stderr output: libswresample 2. 0.101 / 2. 0.101
Stderr output: libpostproc 54. 0.100 / 54. 0.100
Stderr output: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\ErraticFox\Desktop\me.mp4':
Stderr output: Metadata:
Stderr output: major_brand : mp42
Stderr output: minor_version : 0
Stderr output: compatible_brands: mp41isom
Stderr output: creation_time : 2016-06-08 04:59:12
Stderr output: date : 2016
Stderr output: Duration: 00:00:08.22, start: 0.000000, bitrate: 5677 kb/s
Stderr output: Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt470m), 1920x1080 [SAR 1:1 DAR 16:9], 5483 kb/s, 58.66 fps, 59.94 tbr, 60k tbn (default)
Stderr output: Metadata:
Stderr output: creation_time : 2016-06-08 04:59:12
Stderr output: handler_name : VideoHandler
Stderr output: encoder : AVC Coding
Stderr output: Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 186 kb/s (default)
Stderr output: Metadata:
Stderr output: creation_time : 2016-06-08 04:59:12
Stderr output: handler_name : SoundHandler
Stderr output: [mp3 @ 0000000002b7f020] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
Stderr output: Output #0, mp3, to 'C:\Users\ErraticFox\Desktop\output.mp3':
Stderr output: Metadata:
Stderr output: major_brand : mp42
Stderr output: minor_version : 0
Stderr output: compatible_brands: mp41isom
Stderr output: TDRC : 2016
Stderr output: TSSE : Lavf57.37.101
Stderr output: Stream #0:0(und): Audio: mp3, 48000 Hz, stereo, fltp (default)
Stderr output: Metadata:
Stderr output: creation_time : 2016-06-08 04:59:12
Stderr output: handler_name : SoundHandler
Stderr output: encoder : Lavc57.43.100 libmp3lame
Stderr output: Stream mapping:
Stderr output: Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
Stderr output: Press [q] to stop, [?] for help
Stderr output: size= 128kB time=00:00:08.16 bitrate= 128.3kbits/s speed=71.8x
progress undefined%
Stderr output: video:0kB audio:128kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.265012%
Stderr output:
file has been converted succesfully
UPDATE: So I got it to work, _somewhat_.
This is how I'm showing the percentage. Unfortunately, this only works when converting to mp3. Any reason why this is?
.on('stderr', function(line) {
var res = inputDir.split(" ")
if (line.trim().startsWith('Duration')) {
var match = line.trim().match(/Duration:\s\d\d\:\d\d:\d\d/).toString().split('Duration:').slice(1).toString().split(':');
duration = +match[0] * 60 * 60 + +match[1] * 60 + +match[2];
} else if (line.trim().startsWith('size=')) {
match = line.trim().match(/time=\d\d\:\d\d:\d\d/).toString().split('time=').slice(1).toString().split(':');
var seconds = +match[0] * 60 * 60 + +match[1] * 60 + +match[2];
percent = seconds / duration;
percent = (percent * 100).toFixed()
$('#percent').html(percent + '%')
$('.determinate').css('width', percent + '%')
console.log(`stderr: ${line}`)
console.log(`${percent}%`)
}
})
I _think_ the reason is just that your first example may be too short for a progress event.
The conversion is too short? I don't think that would be that case, considering I'm using a two plus hour long movie to test this haha.
It seems like I have the same kind of problem, with a small variation, the 1st video I transcode got the percent correctly set but not the next. If I restart my node app it works correctly again for the 1st video and not the following...
@gregaubert are you using a different command instance ?
No I'm using a different command instance but with nearly the same options except the source video and target
maybe its something to do with the latest ffmpeg version !!
@ThaCoderr I don't think so, but I need to investigate this further (don't have much time though)
The progress is calculated with the duration of the media using ffprobe command (here and here) this only executes ffprobe and saves the data in _ffprobeData, asynchronously, the duration is extracted here so if ffprobe executes after this, duration is 0, thus making porcentage here undefined.
One way to solve this is moving the duration calculation to the utils.extractProgress funciton:
// duration is not needed now
extractProgress: function(command, stderrLine) {
var progress = parseProgressLine(stderrLine);
if (progress) {
// build progress report object
var ret = {
frames: parseInt(progress.frame, 10),
currentFps: parseInt(progress.fps, 10),
currentKbps: progress.bitrate ? parseFloat(progress.bitrate.replace('kbits/s', '')) : 0,
targetSize: parseInt(progress.size, 10),
timemark: progress.time
};
// calculate percent progress using _ffprobeData when available
if (command._ffprobeData && command._ffprobeData.format && command._ffprobeData.format.duration) {
var duration = Number(command._ffprobeData.format.duration);
if (!isNaN(duration))
ret.percent = (utils.timemarkToSeconds(ret.timemark) / duration) * 100;
}
command.emit('progress', ret);
}
}
calculating percentage uses ffprobe, if you have installed ffmpeg via node-ffmpeg-installer, you should install node-ffprobe-installer too
Most helpful comment
I too get
progress undefined%when doing this:What am I doing wrong?