fluent-ffmpeg / node-fluent-ffmpeg

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

Converting a piece of video from mkv to mp4 using fluent-ffmpeg #1202

Closed platinum355 closed 1 year ago

platinum355 commented 1 year ago

Hi! I am trying to stream video from server to browser. There was a problem when rewinding the video, because the entire file is not on the server, it is in the process of downloading from another server and converting (from mkv/matroska to mp4). But I can control the file download flow on the server (set the download priority for a piece from N bytes to M bytes). If this is a piece from 0 to M bytes, then the conversion is successful, but if the piece does not start from 0 bytes, then there is no video in the output file and the sound is distorted. I suspect that the problem is the missing key frame at the beginning of the piece. Internet searches and the use of different combinations of ffmpeg settings was unsuccessful. What could be the problem ? I'm not good with video containers. Maybe there are headers at the beginning of the source file, without which successful conversion is impossible?

Here is part of the code (as a test I am trying to convert part of the video from my computer):

const stream = fs.createReadStream('./test.mkv', {start: 1e7, end: 2e7});

ffmpeg(stream)
    .inputOptions([
        '-ss 00:00:00'
    ])
    .outputFormat('mp4')
    .outputOptions([
        '-c:v copy',
        '-c:a libmp3lame',
        '-movflags +frag_keyframe+faststart',
    ])
    .on('end', function() {
        console.log('file has been converted succesfully');
    })
    .on('error', function(err) {
        console.log('an error happened: ' + err.message);
    })
    .save('./testOut.mp4');

I tried using the ffmpeg settings to find the keyframe and analyzed the video with ffprobe, found out the offset of a random keyframe in bytes and tried to upload it, but also without success.