FFmpeg添加字幕时自定义字体、字号和文字颜色的方法

2022年05月20日

原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/4153.html

我是在使用迅捷视频工具的时候,因为我知道迅捷是使用FFmpeg进行操作的,所以我也想知道如何操作。

FFmpeg转码、加载字幕等操作分开来操作的话,我都懂得。

但是如果这些功能全部连起来一起操作,命令行要如何写呢?我很好奇,所以折腾了两天,请教了好多大佬。

最后在Telegram上的一个技术交流群中,有两个大佬给出了正确的指点。

一、加载字幕(指定字体 字号大小 文字颜色)
一般来说,FFmpeg加载字幕时使用到了两个滤镜模块,一个是 “subtitles”,另一个是 “drawtext”,在制作视频水印的时候,更多的是使用“drawtext”,而制作srt和ass等字幕文件的时候,我们可以使用 “subtitles”。

D:\ffmpeg\bin\ffmpeg.exe -i 20220518142237.mp4 -vf "subtitles=20220518142237.srt:fontsdir=.:force_style='PrimaryColour=&H2cc1a4,FontName=宋体,FontSize=10',scale=1080:1920" -vcodec libx264 -b:v 20000k -acodec aac -b:a 320k -ar 48000 -ac 2 D:\a\20220518142237.mp4
#注意事项
#运行命令的时候,尽可能用cd切换到该视频、字幕文件、字体文件的目录(三个文件最好放在同一个目录)
#20220518142237.mp4 是需要编辑的视频文件
#20220518142237.srt 是需要的字幕文件 如何创建srt字幕文件,请搜索本博客
#fontsdir=. 是字体存放目录
#PrimaryColour=&H2cc1a4 是字体颜色
#FontName=宋体 是字体
#FontSize=10 是文字大小
#scale=1080:1920 同时转码视频像素为1080:1920
#-vcodec libx264 视频转码为H264
#-b:v 20000k 比特率为20000kbps
#-acodec aac 音频转码为aac
#-b:a 320k 音频比特率转码为320kbps
#-ar 48000 音频取样频率为48000hz
#-ac 2 双声道

注意事项:命令中的所有符号均为半角符号。

错误提示:Multiple -filter, -af or -vf options specified for stream 0, only the last option ‘-filter:v scale=1080:1920’ will be used.

出现了上述的错误提示的话,是我们命令中写入了多个 -vf -af 参数,也就是说我们提供了两个滤镜。我们可以把subtitles滤镜和scale滤镜放在一条-vf参数里的,用逗号隔开。

subtitles滤镜参数

例:ffmpeg -i "imput.mp4" -lavfi "subtitles=subtitles.srt:force_style='Alignment=0,OutlineColour=&H100000000,BorderStyle=3,Outline=1,Shadow=0,Fontsize=18,MarginL=5,MarginV=25'" -crf 1 -c:a copy "output.mp4"

01.Name             风格(Style)的名称. 区分大小写. 不能包含逗号.
02.Fontname         使用的字体名称, 区分大小写.
03.Fontsize         字体的字号
04.PrimaryColour    设置主要颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR. 为字幕填充颜色
05.SecondaryColour  设置次要颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR. 在卡拉OK效果中由次要颜色变为主要颜色.
06.OutlineColour    设置轮廓颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR.
07.BackColour       设置阴影颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR. ASS的这些字段还包含了alpha通道信息. (AABBGGRR), 注ASS的颜色代码要在前面加上&H
08.Bold             -1为粗体, 0为常规
09.Italic           -1为斜体, 0为常规
10.Underline       [-1 或者 0] 下划线
11.Strikeout       [-1 或者 0] 中划线/删除线
12.ScaleX          修改文字的宽度. 为百分数
13.ScaleY          修改文字的高度. 为百分数
14.Spacing         文字间的额外间隙. 为像素数
15.Angle           按Z轴进行旋转的度数, 原点由alignment进行了定义. 可以为小数
16.BorderStyle     1=边框+阴影, 3=纯色背景. 当值为3时, 文字下方为轮廓颜色的背景, 最下方为阴影颜色背景.
17.Outline         当BorderStyle为1时, 该值定义文字轮廓宽度, 为像素数, 常见有0, 1, 2, 3, 4.
18.Shadow          当BorderStyle为1时, 该值定义阴影的深度, 为像素数, 常见有0, 1, 2, 3, 4.
19.Alignment       定义字幕的位置. 字幕在下方时, 1=左对齐, 2=居中, 3=右对齐. 1, 2, 3加上4后字幕出现在屏幕上方. 1, 2, 3加上8后字幕出现在屏幕中间. 例: 11=屏幕中间右对齐. Alignment对于ASS字幕而言, 字幕的位置与小键盘数字对应的位置相同.
20.MarginL         字幕可出现区域与左边缘的距离, 为像素数
21.MarginR         字幕可出现区域与右边缘的距离, 为像素数
22.MarginV         垂直距离

 

二、SecondaryColour参数
在我测试的时候,在命令中添加了SecondaryColour参数,但是并没有起作用,字幕并没有像卡拉OK中的字幕一样出现变化,用Telegram上大佬说的,这个参数需要有两行字幕在同一个位置才会发生变化,我没有试过,有懂的朋友也请指点一下。
相关链接:https://www.myzhenai.com.cn/post/3634.html


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.165.248.212
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://www.myzhenai.com/post/4153.html

8条评论

  • 海南胡说 2022年06月1日在12:25 上午

    设置水印每隔10秒钟从左移动右直至消失
    ffmpeg -i big_buck_bunny.mp4 -ignore_loop 0 -i doggie3.gif -lavfi “overlay=enable=’mod(t,10)’:x=’100*mod(t,10)-w'” -shortest out.mp4 -y

    一键批量加水印,
    https://blog.csdn.net/qq_33474442/article/details/87871737

    提取视频截图水印坐标的工具
    https://github.com/CloudTranscoding/ffmpeg-watermark-position-extraction-tools

    subtitles 滤镜的详细用法:
    http://ffmpeg.org/ffmpeg-all.html#subtitles-1

    给视频添加封面
    ffmpeg -i in.mp4 -i IMAGE -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic out.mp4

    同时添加图片水印和文字水印
    ffmpeg -re -i input.mp4 -ignore_loop 0 -i 1.gif -filter_complex “overlay=5:H-h-5[file1];[file1]drawtext=text=’Text Content’:fontsize=80:box=1:x=1000:y=1800” -shortest -c:v libx264 -c:a aac -b:a 192k -strict -2 -y output.mp4

  • 海南胡说 2022年06月14日在9:52 上午

    ffmpeg -i in.mp4 -filter_complex “scale=-2:720,pad=1280:720:280:0:red” -y out.mp4
    ffmpeg -i in.mp4 -vf “scale=-2:720,pad=1280:720:280:0:red” -y out.mp4
    视频旋转

  • 海南胡说 2022年07月22日在10:10 上午

    添加环绕的水印(飞鸟、走马灯)

    ffmpeg.exe -i input.mp4 -vf drawtext=”fontsize=10:fontfile=/Windows/Fonts/arial.ttf:text=’Text Here’:x=if(eq(mod(n\,1200)\,0)\,rand(0\,(w-text_w))\,x):y=if(eq(mod(n\,1200)\,0)\,rand(0\,(h-text_h))\,y):enable=lt(mod(n\,1200)\,200)” -c:v libx264 -crf 17 -c:a copy output.mp4

    ffmpeg -i input.mp4 -loop 1 -i logo.png -filter_complex “[1]trim=0:30,fade=in:st=0:d=1:alpha=1,fade=out:st=9:d=1:alpha=1,loop=999:750:0,setpts=N/25/TB[w];[0][w]overlay=shortest=1:x=if(eq(mod(n\,200)\,0)\,sin(random(1))*W\,x):y=if(eq(mod(n\,200)\,0)\,sin(random(1))*H\,y)” output.mpg

  • 海南胡说 2022年08月13日在9:56 下午

    ffmpeg调用中文字体写法

    fileQueue.add(() async {
    final oldFile = ‘${file.filePath}_’;
    await File(file.filePath).rename(oldFile);
    final command = [
    ‘ffmpeg -i “$oldFile”‘,
    ‘-vf “drawtext=fontfile=\’$font\’: text=\’$watermark\”,
    ‘:x=$x: y=$y: fontsize=20: fontcolor=white@0.8: box=1: boxcolor=random@0.5: boxborderw=5″‘,
    ‘-c:a copy -c:v ${info.codec_name} -profile:v ${info.profile} -level ${info.level}’,
    ‘${file.filePath} -y’,
    ].join(‘ ‘);
    // debugPrint(command);
    return run(command, verbose: false);
    });
    });
    final toast = ProcessingToastWidget.showQueue(‘添加文字水印’, fileQueue);
    await fileQueue.whenComplete();
    toast.dismiss();
    }

  • 海南胡说 2022年09月7日在12:02 下午

    drawbox滤镜

    PS C:\Users\Administrator> cd D:\
    PS D:\> ffmpeg/bin/ffplay.exe -h filter=drawbox
    ffplay version N-102564-g2261cc6d8a Copyright (c) 2003-2021 the FFmpeg developers
    built with gcc 10-win32 (GCC) 20210408
    configuration: –prefix=/ffbuild/prefix –pkg-config-flags=–static –pkg-config=pkg-config –cross-prefix=x86_64-w64-
    mingw32- –arch=x86_64 –target-os=mingw32 –enable-gpl –enable-version3 –disable-debug –disable-w32threads –enable-
    pthreads –enable-iconv –enable-libxml2 –enable-zlib –enable-libfreetype –enable-libfribidi –enable-gmp –enable-lz
    ma –enable-fontconfig –enable-libvorbis –enable-opencl –enable-libvmaf –enable-vulkan –enable-amf –enable-libaom
    –enable-avisynth –enable-libdav1d –enable-libdavs2 –enable-ffnvcodec –enable-cuda-llvm –enable-libglslang –enable
    -libgme –enable-libass –enable-libbluray –enable-libmp3lame –enable-libopus –enable-libtheora –enable-libvpx –ena
    ble-libwebp –enable-lv2 –enable-libmfx –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libopenjpeg –en
    able-librav1e –enable-librubberband –enable-schannel –enable-sdl2 –enable-libsoxr –enable-libsrt –enable-libsvtav1
    –enable-libtwolame –enable-libuavs3d –enable-libvidstab –enable-libx264 –enable-libx265 –enable-libxavs2 –enable
    -libxvid –enable-libzimg –extra-cflags=-DLIBTWOLAME_STATIC –extra-cxxflags= –extra-ldflags=-pthread –extra-ldexefla
    gs= –extra-libs=-lgomp
    libavutil 57. 0.100 / 57. 0.100
    libavcodec 59. 1.100 / 59. 1.100
    libavformat 59. 2.100 / 59. 2.100
    libavdevice 59. 0.100 / 59. 0.100
    libavfilter 8. 0.101 / 8. 0.101
    libswscale 6. 0.100 / 6. 0.100
    libswresample 4. 0.100 / 4. 0.100
    libpostproc 56. 0.100 / 56. 0.100
    Filter drawbox
    Draw a colored box on the input video.
    Inputs:
    #0: default (video)
    Outputs:
    #0: default (video)
    drawbox AVOptions:
    x ..FV…..T. set horizontal position of the left box edge (default “0”)
    y
    ..FV…..T. set vertical position of the top box edge (default “0”)
    width
    ..FV…..T. set width of the box (default “0”)
    w
    ..FV…..T. set width of the box (default “0”)
    height
    ..FV…..T. set height of the box (default “0”)
    h
    ..FV…..T. set height of the box (default “0”)
    color
    ..FV…..T. set color of the box (default “black”)
    c
    ..FV…..T. set color of the box (default “black”)
    thickness
    ..FV…..T. set the box thickness (default “3”)
    t
    ..FV…..T. set the box thickness (default “3”)
    replace ..FV…..T. replace color & alpha (default false)

    This filter has support for timeline through the ‘enable’ option.

  • 海南胡说 2022年09月7日在12:04 下午

    overlay滤镜

    PS D:\> ffmpeg/bin/ffplay.exe -h filter=overlay
    ffplay version N-102564-g2261cc6d8a Copyright (c) 2003-2021 the FFmpeg developers
    built with gcc 10-win32 (GCC) 20210408
    configuration: –prefix=/ffbuild/prefix –pkg-config-flags=–static –pkg-config=pkg-config –cross-prefix=x86_64-w64-
    mingw32- –arch=x86_64 –target-os=mingw32 –enable-gpl –enable-version3 –disable-debug –disable-w32threads –enable-
    pthreads –enable-iconv –enable-libxml2 –enable-zlib –enable-libfreetype –enable-libfribidi –enable-gmp –enable-lz
    ma –enable-fontconfig –enable-libvorbis –enable-opencl –enable-libvmaf –enable-vulkan –enable-amf –enable-libaom
    –enable-avisynth –enable-libdav1d –enable-libdavs2 –enable-ffnvcodec –enable-cuda-llvm –enable-libglslang –enable
    -libgme –enable-libass –enable-libbluray –enable-libmp3lame –enable-libopus –enable-libtheora –enable-libvpx –ena
    ble-libwebp –enable-lv2 –enable-libmfx –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libopenjpeg –en
    able-librav1e –enable-librubberband –enable-schannel –enable-sdl2 –enable-libsoxr –enable-libsrt –enable-libsvtav1
    –enable-libtwolame –enable-libuavs3d –enable-libvidstab –enable-libx264 –enable-libx265 –enable-libxavs2 –enable
    -libxvid –enable-libzimg –extra-cflags=-DLIBTWOLAME_STATIC –extra-cxxflags= –extra-ldflags=-pthread –extra-ldexefla
    gs= –extra-libs=-lgomp
    libavutil 57. 0.100 / 57. 0.100
    libavcodec 59. 1.100 / 59. 1.100
    libavformat 59. 2.100 / 59. 2.100
    libavdevice 59. 0.100 / 59. 0.100
    libavfilter 8. 0.101 / 8. 0.101
    libswscale 6. 0.100 / 6. 0.100
    libswresample 4. 0.100 / 4. 0.100
    libpostproc 56. 0.100 / 56. 0.100
    Filter overlay
    Overlay a video source on top of the input.
    slice threading supported
    Inputs:
    #0: main (video)
    #1: overlay (video)
    Outputs:
    #0: default (video)
    overlay AVOptions:
    x ..FV……. set the x expression (default “0”)
    y
    ..FV……. set the y expression (default “0”)
    eof_action ..FV……. Action to take when encountering EOF from secondary input (from 0 to 2) (d
    efault repeat)
    repeat 0 ..FV……. Repeat the previous frame.
    endall 1 ..FV……. End both streams.
    pass 2 ..FV……. Pass through the main input.
    eval
    ..FV……. specify when to evaluate expressions (from 0 to 1) (default frame)
    init 0 ..FV……. eval expressions once during initialization
    frame 1 ..FV……. eval expressions per-frame
    shortest ..FV……. force termination when the shortest input terminates (default false)
    format ..FV……. set output format (from 0 to 7) (default yuv420)
    yuv420 0 ..FV…….
    yuv420p10 1 ..FV…….
    yuv422 2 ..FV…….
    yuv422p10 3 ..FV…….
    yuv444 4 ..FV…….
    rgb 5 ..FV…….
    gbrp 6 ..FV…….
    auto 7 ..FV…….
    repeatlast ..FV……. repeat overlay of the last overlay frame (default true)
    alpha ..FV……. alpha format (from 0 to 1) (default straight)
    straight 0 ..FV…….
    premultiplied 1 ..FV…….

    framesync AVOptions:
    eof_action ..FV……. Action to take when encountering EOF from secondary input (from 0 to 2) (d
    efault repeat)
    repeat 0 ..FV……. Repeat the previous frame.
    endall 1 ..FV……. End both streams.
    pass 2 ..FV……. Pass through the main input.
    shortest ..FV……. force termination when the shortest input terminates (default false)
    repeatlast
    ..FV……. extend last frame of secondary streams beyond EOF (default true)

    This filter has support for timeline through the ‘enable’ option.
    PS D:\>

  • 海南胡说 2022年09月15日在2:14 下午

    ffmpeg -i ee.mp4 -i logo.png -filter_complex ‘overlay=main_w-overlay_w-10:main_h-overlay_h-10’ output.mp4
    使用图片来遮挡某个区域?

  • 海南胡说 2022年09月29日在4:26 下午

    D:\ffmpeg\bin\ffmpeg.exe -i a.mp4 -vf “transpose=2,transpose=2,transpose=2,transpose=2,scale=1080:1920” -vcodec libx264 -b 20000k -acodec aac -ab 320k -ar 48000 -ac 2 dmangshe.mp4 #横屏转竖屏

回复 海南胡说 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注