【问题标题】:How to apply gradient color using FFMPEG library where color fades away to a transparent color from a specific color?如何使用 FFMPEG 库应用渐变颜色,其中颜色从特定颜色逐渐消失为透明颜色?
【发布时间】:2021-12-30 05:43:51
【问题描述】:

我想要类似上图的东西。

我试过这个代码

ffmpeg -i input.mp4 -vf 
"drawtext=fontfile=Alaska.ttf:text='demo text':fontcolor=black:fontsize=30:box=1
:boxborderw=5:boxcolor=0x483D8B:boxcolor=0x2F4F4F:x=1:y=550:enable='between(t,14,t)'"
-codec:a copy output.mp4

【问题讨论】:

    标签: ffmpeg terminal


    【解决方案1】:

    为此,首先在trans.png上添加渐变色和需要用php写在视频上的文字。

    这里是trans.png:



            //for video gradient bg
            $name='tmp.png';
            $name_count = strlen($vName);
            $start = 0;
            $stop = $name_count * 20;
            $color_trans = '8F7E9B';
    
            // if (isset($start) && isset($stop) && isset($color)) {
            $color = hex2rgb($color_trans);
            $range = $stop - $start;
    
            // create input image
            $input = imagecreatefrompng(PATH . 'trans.png');
    
    
            // create output image
            $height = imagesy($input);
            $height = 45;
            $width = imagesx($input);
            $output = imagecreatetruecolor($width, $height);
    
            // put a transparent background on it
            $trans_colour = imagecolorallocatealpha($output, 0, 0, 0, 127);
            imagefill($output, 0, 0, $trans_colour);
    
            // create the gradient
            for ($x = 0; $x < $width; ++$x) {
                $alpha = $x <= $start ? 0 : round(min(($x - $start) / $range, 1) * 127);
                
                $new_color = imagecolorallocatealpha($output, $color[0], $color[1], $color[2], $alpha);
                
                imageline($output, $x, $height, $x, 0, $new_color);
            } // copy the gradient onto the input image 
            imagecopyresampled($input, $output, 0, 0, 0, 0, $width, $height, $width, $height);
            // output the result 
            // header('Content-Type: image/png');
    
            $name = 'trans_' . rand(10, 100) . '.png';
            imagepng($input, PATH . 'temp/' . $name);
            // }
            //--/for video gradient bg
    

    然后将此图像与您的视频合并

    ffmpeg -i input.mp4 ' -i ' . PATH . 'temp/' . $name . ' -filter_complex "[0:v][1:v]overlay=0:500:enable=between(t\,4\,t)" -codec:a copy ' . PATH . 'videos/video.mp4 
    

    【讨论】:

    • 您需要非常小心这段代码...就目前而言,您允许在命令行中输入任意数据。可能存在严重的安全问题。
    • 你能建议我如何在没有安全问题的情况下做到这一点@Brad
    猜你喜欢
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多