美文网首页Linux学习之路Linuxffmpeg
使用ffmpeg压制硬字幕与logo

使用ffmpeg压制硬字幕与logo

作者: EricKwoc | 来源:发表于2018-04-29 19:03 被阅读21次

这个需求其实挺变态的
我之前压制硬字幕的命令是
ffmpeg -i input.mp4 -vf "ass=input.ass" output.mp4
压制logo的命令是
ffmpeg -i input.mp4 -i logo.ico -filter_complex overlay output.mp4

于是我想当然地把这两条语句结合
ffmpeg -i input.mp4 -i logo.ico -filter_complex overlay -vf "ass=input.ass" output.mp4
结果出现了报错

Filtergraph 'ass=source.ass' was specified through the -vf/-af/-filter option for 
output stream 0:0, which is fed from a complex filtergraph.
-vf/-af/-filter and -filter_complex cannot be used together for the same stream.

大致意思就是-vf选项不能和-filter_complex滤镜一起使用
头大

然后一番搜索之后,照着这篇的解释和这篇,拼凑出了下面这条命令

ffmpeg \
    -i source.mkv \
    -i Logo_White.ico \
    -filter_complex \
    "[0:v][1:v]overlay[logo];\
    [logo]ass=source.ass[sub]" \
    -map "[sub]" \
    output.final.mp4

我说一下自己的理解
首先看到命令中有两个输入,一个是视频文件,为 input0,一个是 logo 图像,为 input1.
filter_complex 滤镜的参数里面 [0:v]0是 input0,v代表处理的是视频而不是音频,处理音频的待会儿再讲
整体来看,[0:v][1:v]overlay[logo]是一个2输入1输出的管子,管子把 input1 的视频流(此处为一张图)叠加到 input0 的上面,出来的产品叫做[logo]
然后下一句[logo]ass=source.ass[sub]中,可以把中间那个ass滤镜当做一个1输入1输出的管子,这个管子对每个视频流进行处理,在视频上打上字幕,出来的产品叫做[sub]
最终把这个[sub] 映射到 output.final.mp4 上面

这样处理出来的视频logo和字幕都有了,但还有一个问题:音频流全部丢失,处理出来的是无声视频,大致猜测是由于我们只处理了视频流,导致音频流丢失。

又是一番搜索,终于在这里看到了一点蛛丝马迹

-map 0:0: add the first input's first stream to the output (which is the video)

也就是把第一个输入(input0)的第一个音轨(stream0)映射到输出中

手残的我在命令里加入了一句-map 0,0

ffmpeg \
    -i source.mkv \
    -i Logo_White.ico \
    -filter_complex \
    "[0:v][1:v]overlay[logo];\
    [logo]ass=source.ass[sub]" \
    -map [sub] \
    -map 0,0 \
    output.final.mp4

误打误撞居然有声音了
我也不知道为什么,难道说是我的版本把这个命令的用法改了??有人知道吗

附上我的环境

ffmpeg version 3.3.2-1 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 6.4.0 (Debian 6.4.0-2) 20170724
  configuration: --prefix=/usr --extra-version=1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100

相关文章

网友评论

  • 歡爺:小白问
    我在手机上想把srt或ass字幕压进mp4变成硬字幕 使用 -vf subtitles=/sdcard/1.srt 参数 或者-vf "ass=/sdcard/1.ass"这样的参数 但是压完的mp4上完全不显示 没有效果 请问应该怎么弄?
    请问是不是缺少什么函式库?还是说安卓系统缺少什么支持了?
    我使用的是两个现成的app 主要是”FFFmpeg 命令列” 另一个是“FFmpeg Media Encoder”
    EricKwoc:@歡爺 按照错误提示来看,应该是软件作者为了轻量化,把这些滤镜阉割掉了,如果需要的话可能要自行编译……操作的细节我不是很清楚,你可以谷歌一下
    歡爺:@歡爺 subtitles 描述: 该滤镜调用libass库,讲字幕添添加到输入视频中。如果要使用该滤镜,需要在编译FFmpeg时使用--enable-libass配置项。这个滤镜需要配合使用 libavcodec和libavformat将输入的字幕文件转换为ASS

    请问怎么激活libass库???

本文标题:使用ffmpeg压制硬字幕与logo

本文链接:https://www.haomeiwen.com/subject/gmhalftx.html