Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watermark, concatenate, fade-in, get fps... all in one... but how? #1078

Open
mlhyyl opened this issue Oct 30, 2020 · 2 comments
Open

Watermark, concatenate, fade-in, get fps... all in one... but how? #1078

mlhyyl opened this issue Oct 30, 2020 · 2 comments

Comments

@mlhyyl
Copy link

mlhyyl commented Oct 30, 2020

Hi,

I'm experimenting on adapting an ffmpeg commmand to node-fluent-ffmpeg. What my command does is

  • creating a 15 seconds long disclaimer from a jpg, which is going to be added to a lecture video.
  • trimming the lecture video at the beginning and end
  • adding a watermark to the upper left corner
  • adding a one-second fade-in at the beginning
  • adding a one-second fade-out at the end
  • concatenating both videos

Unfortunately, I was only able to achieve the trimming part or the add logo part but not both the same time. I also wasn't able to retrieve the fps of the lecture video which would be determining the fps of the disclaimer video.

So here goes my code:

ffmpeg -i HIST-192-WEEK-6.mp4 -i logo.png -i disclaimer.jpg -f lavfi -t 1 -i anullsrc -filter_complex "[2:v]fps=fps=30,loop=loop=450:size=30:start=0[v2];[v2]fade=t=in:st=0:d=1,fade=t=out:st=14:d=1[vv2];[0:v]trim=start=1:end=5771,setpts=PTS-STARTPTS,scale=1920:1080[v0];[0:a]atrim=start=1:end=5771,asetpts=PTS-STARTPTS[a0];[v0][1:v]overlay=0:0[vv0];[vv0]fade=t=in:st=0:d=1,fade=t=out:st=5769:d=1[vvv0];[a0]afade=t=in:st=0:d=1,afade=t=out:st=5769:d=1[aa0];[vv2][3:a][vvv0][aa0]concat=n=2:v=1:a=1[vv][aa]" -map [vv] -map [aa] -c:a aac -vcodec libx264 -s 1920x1080 output.HIST-192-WEEK-6.mp4

I hope I'm not asking too much.

Thanks anyway.

@mlhyyl
Copy link
Author

mlhyyl commented Oct 30, 2020

I made some progress. Except for the first and last steps, I managed to do the rest.

In order to obtain the frame rate, I had to run ffmpeg within ffprobe.

What is missing is the part where I add an empty audio track. I wasn't able to fit "-f lavfi -t 1 -i anullsrc" into the code.

Any help/advice appreciated.

Here goes my code:

ffmpeg.ffprobe("HIST-192-WEEK-6.mp4", function (err, metadata) {
        frameRateFPSRaw = metadata.streams[0].r_frame_rate;
        frameRateFPS = frameRateFPSRaw.split("/");
        fpsValue = frameRateFPS[0] / frameRateFPS[1];
        let fadeOutFrame = Math.round(fadeOutTime * frameRateFPS[0] / frameRateFPS[1]);
        if (err) {
            console.log(err);
        } else {
            console.log(fadeOutFrame);
            ffmpeg()
                .input("HIST-192-WEEK-6.mp4")
                .input("media/logo.png")
                .input("media/disclaimer.png")
                .input() // something something to create a dummy audio which will be called as [3:a] during concatenation
                .complexFilter([
                    '[0:v]trim=start=' + startSecond + ':end=' + endSecond + ',setpts=PTS-STARTPTS[trimmedV]',
                    '[0:a]atrim=start=' + startSecond + ':end=' + endSecond + ',asetpts=PTS-STARTPTS[trimmedA]',
                    '[1:v]setdar=16:9[watermark]',
                    '[trimmedV][watermark]overlay=0:0[trimmedBrandedV]',
                    '[trimmedBrandedV]fade=t=in:st=0:d=1,fade=t=out:st=' + fadeOutTime + ':d=1[trimmedBrandedFadedV]',
                    '[trimmedA]afade=t=in:st=0:d=1,afade=t=out:st=' + fadeOutTime + ':d=1[trimmedFadedA]',                    
                    '[2:v]fps=fps='+fpsValue+',loop=loop='+(Math.round(fpsValue*15))+':size='+fpsValue+':start=0[v2]',
                    '[v2]fade=t=in:st=0:d=1,fade=t=out:st=14:d=1[disclaimerV]',                    
                    '[disclaimerV][3:a][trimmedBrandedFadedV][trimmedFadedA]concat=n=2:v=1:a=1[vv][aa]' //
                ])
                .aspect('16:9')
                .outputOptions([
                    '-map [vv]',
                    '-map [aa]',
                    '-vcodec libx264',
                    '-s 1920x1080'
                ])
                .withOutputFormat(to)
                .on('progress', function (progress) {
                    frames = progress.frames;
                    timemark = progress.timemark;
                    console.log('currentFps:' + progress.currentFps + ' Processing: ' + progress.frames + '/' + (durationSeconds * Math.round(fpsValue)) + ' timemark: ' + progress.timemark);
                })
                .on("end", function (stdout, stderr) {
                    console.log("Finished");
                    res.download(__dirname + fileName, function (err) {
                        if (err) throw err;
                        fs.unlink(__dirname + fileName, function (err) {
                            if (err) throw err;
                            console.log("File deleted");
                        });
                    });
                    fs.unlink("tmp/" + file.name, function (err) {
                        if (err) throw err;
                        console.log("File deleted");
                    });
                })
                .on("error", function (err) {
                    console.log("an error happened: " + err.message);
                    fs.unlink("tmp/" + file.name, function (err) {
                        if (err) throw err;
                        console.log("File deleted");
                    });
                })
                .saveToFile(__dirname + fileName);;
        }
    });

@stepin
Copy link

stepin commented Oct 9, 2024

-f lavfi part is related to #1282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants