使用ffmpeg压制硬字幕与logo

2018.9.17更新
命令改成这样,不然将会压制出来两条视频轨道,一条有字幕一条没有 = =

ffmpeg.exe \
-i %video% \
-i %Logo% \
-filter_complex "[1]scale=100:100[logo];\
[0][logo]overlay=%pos%[taged];\
[taged]ass='%ass%'[sub]" \
-map [sub] -map 0:a %output%

btw, [1]scale=100:100[logo];这一句顾名思义是缩放的意思

%pos%可以选下面这几种

TOP_LEFT='10:10'
TOP_RIGHT='main_w-overlay_w-10:10'
BUTTOM_LEFT='10:main_h-overlay_h-10'
BUTTOM_RIGHT='main_w-overlay_w-10:main_h-overlay_h-10'

这个需求其实挺变态的
我之前压制硬字幕的命令是
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
    原文作者:EricKwoc
    原文地址: https://www.jianshu.com/p/e2fa5e17eacc
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞