ffmpeg - 如何在不对视频应用图像蒙版的情况下创建圆形视频(视频顶部的透明区域)效果

标签 ffmpeg mp4 mask

基本上我搜索了很多,解决方案建议应用一些 PNG 掩码或不提供所需的解决方案。
我发现了什么。

ffmpeg -i main.mkv -i facecloseup.mkv
 -filter_complex "[1]trim=end_frame=1,
  geq='st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),pow(min(W/2,H/2),2)),255,0)':128:128,
  loop=-1:1,setpts=N/FRAME_RATE/TB[mask];
  [1][mask]alphamerge[cutout];
  [0][cutout]overlay=x=W-w:y=0[v];
  [0][1]amix=2[a]"
 -map "[v]" -map "[a]"  out.mp4
command = "-i " + this.video1Path.getPath() + " -i " + this.video2Path.getPath() + " -filter_complex [1]trim=end_frame=1,geq=lum_expr='st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3)," + (this.mZoomLayout.getZoomedWidth()/2) + "*" + (this.mZoomLayout.getZoomedWidth()/2) + "),255,0)':128:128,format=gray,loop=-1:1,setpts=N/FRAME_RATE/TB[mask];[1][mask]alphamerge,format=rgba,lutrgb=a=if(gte(val\\,16)\\,val)[cutout];[0][cutout]overlay=" + this.mZoomLayout.getCircleX() + ":" + this.mZoomLayout.getCircleY() + ":enable='between(t,0," + this.videoTwoDuration + ") -c:v libx264 -crf 24 -preset ultrafast " + videoPath.getPath(); 
所以我试图从他们那里提取需要的东西,但我不明白我到底需要怎么做,我这样做了:
ffmpeg -i video.mp4 -filter_complex "[0]geq='st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),pow(min(W/2,H/2),2)),255,0)':H:W; [0:v][mask]alphamerge" out.mp4
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001f761dd8e40] Invalid stream specifier: mask.
    Last message repeated 1 times
Stream specifier 'mask' in filtergraph description [0]geq='st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),pow(min(W/2,H/2),2)),255,0)':H:W; [0:v][mask]alphamerge matches no streams.
ffmpeg -i video.mp4 -filter_complex "[0]geq=lum_expr='st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),pow(min(W/2,H/2),2)),255,0)':H:W; [0:v][mask]alphamerge" out.mp4
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001bfd9218e80] Invalid stream specifier: mask.
    Last message repeated 1 times
Stream specifier 'mask' in filtergraph description [0]geq=lum_expr='st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),pow(min(W/2,H/2),2)),255,0)':H:W; [0:v][mask]alphamerge matches no streams.
再一次,伙计们,如果您要发布一些“准备好的图像蒙版”解决方案 - 离开吧,问题是关于在空中创建蒙版。
所以,假设我们有红色方 block (是的,比例是静态的,总是 1:1),是的,我不能发布它,因为我没有 10 个代表。 (...)。
/image/MsL71.png - 红方格。
/image/aIFEV.png - 圆圈
/image/R8EAx.png - 结果
/image/WtqQg.png - 最后结果
我实际上想从@Gyan 或@llogan 那里得到答案,因为我搜索了很多,只有这两个人知道如何以编程方式制作东西。
更多技术细节:
纵横比是恒定的 - 1:1,宽度和高度应该以自动方式从视频中获取,我们需要创建一个内部有透明圆圈的白色正方形,最终结果必须包含白色背景的“圆形”视频。

最佳答案

您可以使用以下图像复制您的“ 最终结果 ”:

  • red.png
  • blue.png

  • 2 个输入
    red vignette over blue circleheight 的值变量应与源文件的高度匹配。在这种情况下,它是 600 像素。
    red="red.png"
    blue="blue.png"
    
    height=600
    
    ffmpeg \
    -loop 1 -i "${red}" \
    -loop 1 -i "${blue}" \
    -filter_complex "\
    [1]format=yuva444p,geq=lum='p(X,Y)':a='st(1,pow(min(W/2,H/2),2))+st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),ld(1)),255,0)'[circular shaped video];\
    [circular shaped video]scale=w=-1:h=${height}[circular shaped video small];\
    [0][circular shaped video small]overlay" \
    -filter_complex_threads 1 \
    -c:v libx264 \
    -preset ultrafast \
    -t 5 \
    "final_result.mp4"
    
    您只需要 -loop 1使用图像时输入前面的选项。
    如果您使用的是视频,您可能会决定使用哪种音频。
    您可以在我的原始 post 中查看所有详细信息.
    仅 1 个输入
    red vignette over blue circle
    在这里,您只需选择圆形晕影的颜色。
    颜色名称在 documentation 中列出.
    in="blue.png"
    
    vignette_color="red"
    
    ffmpeg \
    -loop 1 -i "${in}" \
    -filter_complex "\
    [0]setsar=1:1,format=yuva444p,drawbox=color=${vignette_color}@1:t=fill[vignette];\
    [0]format=yuva444p,geq=lum='p(X,Y)':a='st(1,pow(min(W/2,H/2),2))+st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),ld(1)),255,0)'[circular shaped video];\
    [vignette][circular shaped video]overlay" \
    -filter_complex_threads 1 \
    -c:v libx264 \
    -preset ultrafast \
    -t 5 \
    "result.mp4"
    
    1 个视频自动平方
    包括音频。抗锯齿晕影边缘。
    enter image description here
    in="snowboarder.mp4"
    
    vignette_color="black"
    
    # edge of vignette can be jagged. 
    # use value between 1 - 2 (e.g. 1.2). 1 doesn't do anything.
    anti_aliasing=2
    
    ffmpeg \
    -i "${in}" \
    -filter_complex "\
    [0:v]crop=ih:ih[video square];[video square]split=2[video square 1][video square 2];\
    [video square 1]setsar=1:1,drawbox=color=${vignette_color}@1:t=fill[vignette];\
    [video square 2]scale=w=iw*"${anti_aliasing}":h=iw*"${anti_aliasing}",format=yuva444p,geq=lum='p(X,Y)':a='st(1,pow(min(W/2,H/2),2))+st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),ld(1)),255,0)'[scaled up circular shaped video];\
    [scaled up circular shaped video]scale=w=iw/"${anti_aliasing}":h=iw/"${anti_aliasing}"[circular shaped video];\
    [vignette][circular shaped video]overlay" \
    -filter_complex_threads 1 \
    -c:v libx264 \
    -c:a aac \
    -preset ultrafast \
    "result_square.mp4"
    
    圆形视频(带透明度的方形视频)
    webm video with alpha channel in Firefox browser
    Chrome 和 Firefox 支持具有透明度(alpha channel )的 WebM 视频。
    棋盘图案不是视频的一部分。它是背景。
    in="snowboarder.mp4"
    
    # edge of vignette can be jagged. 
    # use value between 1 - 2 (e.g. 1.2). 1 doesn't do anything.
    anti_aliasing=2    
    
    ffmpeg \
    -i "${i}" \
    -filter_complex "\
    [0:v]crop=ih:ih[video square];\
    [video square]split=3[black canvas][white canvas][video square];\
    [black canvas]setsar=1:1,drawbox=color=black@1:t=fill[black background];\
    [white canvas]scale=w=iw*"${anti_aliasing}":h=iw*"${anti_aliasing}",format=yuva444p,geq=lum='p(X,Y)':a='st(1,pow(min(W/2,H/2),2))+st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),ld(1)),255,0)',drawbox=color=white@1:t=fill[scaled up white circle];\
    [scaled up white circle]scale=w=iw/"${anti_aliasing}":h=iw/"${anti_aliasing}"[white circle];\
    [black background][white circle]overlay[alpha mask];\
    [video square][alpha mask]alphamerge,format=yuva420p" \
    -filter_complex_threads 1 \
    -c:v libvpx -auto-alt-ref 0 \
    -c:a libvorbis \
    -preset ultrafast \
    "result_square.webm"
    

    关于ffmpeg - 如何在不对视频应用图像蒙版的情况下创建圆形视频(视频顶部的透明区域)效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66710672/

    相关文章:

    python - 如何将 H264 RTP 流从 PCAP 转换为可播放的视频文件

    c - Adobe alchemy 编译器问题 - 它生成 .l.bc 文件,如何将它们转换为 swc 文件?

    parsing - 为什么我不能让手动修改的 MPEG-4 扩展框( block )大小工作?

    android - 如何在android opencv中将 mask 应用于实时相机

    linux - FFmpeg 音频交叉淡入淡出

    node.js - ffmpeg将流转换为jpg缓冲区而不保存磁盘

    ffmpeg转换可执行文件输出 ".MOD: no files in such directory"

    powershell - 如何从使用带有 ffmpeg 的时间戳命名的帧创建视频

    html - <div> 中的圆孔

    javascript - 我可以在 CSS 中的 div 上剪切蒙版文本吗(有或没有 JS 的帮助)?