Search code examples
multithreadingffmpegubuntu-14.04http-live-streamingsubtitle

how to build ffmpeg with burn text on hls output while maintaining the aspect ratio


My objective is to burn a text(watermark) at a particular time range on the outputed hls video, I also have to change just the height and maintain the aspect ratio which I am able to do but the burning in text part is not working. First the ffmpeg I used is for 14.04 ubuntu, details are here. I don't think this build supports subtitle (docs) filter which I could have used for my purpose as described here. when i enter ffmpeg into the terminal this is the output

(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ ffmpeg 
ffmpeg version N-78590-g5590ab4 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab
  libavutil      55. 18.100 / 55. 18.100
  libavcodec     57. 24.103 / 57. 24.103
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 32.100 /  6. 32.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

I tried

(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ ffmpeg -threads 4 -i "input_sintel_trailer_720p.mp4" -profile:v baseline -level 4.0 -vf scale="trunc(360*a/2)*2:360, subtitles='subtitles.srt'"  -start_number 0 -hls_time 10 -hls_list_size 0 -f hls videos/4444/index_4444_360_.m3u8

and got an error

[Parsed_subtitles_1 @ 0x2f1b7e0] Shaper: FriBidi 0.19.6 (SIMPLE) HarfBuzz-ng 0.9.27 (COMPLEX)
[Parsed_subtitles_1 @ 0x2f1b7e0] Unable to open subtitles.srt
[AVFilterGraph @ 0x2f1ac00] Error initializing filter 'subtitles' with args 'subtitles.srt'
Error opening filters!

then I tried this

(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ ffmpeg -threads 4 -i "input_sintel_trailer_720p.mp4" -profile:v baseline -level 4.0 -vf "scale='trunc(360*a/2)*2:360', drawtext:drawtext"  -start_number 0 -hls_time 10 -hls_list_size 0 -f hls videos/4444/index_4444_360_.m3u8

based on an answer here and got this error

[AVFilterGraph @ 0x2695c20] No such filter: 'drawtext:drawtext'
Error opening filters!

I tried a static build too which i got from here but still couldn't make it work. with this static build i tried:

ffmpeg version N-63893-gc69defd Copyright (c) 2000-2014 the FFmpeg developers
  built on Jul 16 2014 05:38:01 with gcc 4.6 (Debian 4.6.3-1)
  configuration: --prefix=/root/ffmpeg-static/64bit --extra-cflags='-I/root/ffmpeg-static/64bit/include -static' --extra-ldflags='-L/root/ffmpeg-static/64bit/lib -static' --extra-libs='-lxml2 -lexpat -lfreetype' --enable-static --disable-shared --disable-ffserver --disable-doc --enable-bzlib --enable-zlib --enable-postproc --enable-runtime-cpudetect --enable-libx264 --enable-gpl --enable-libtheora --enable-libvorbis --enable-libmp3lame --enable-gray --enable-libass --enable-libfreetype --enable-libopenjpeg --enable-libspeex --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-version3 --enable-libvpx
  libavutil      52. 89.100 / 52. 89.100
  libavcodec     55. 66.101 / 55. 66.101
  libavformat    55. 43.100 / 55. 43.100
  libavdevice    55. 13.101 / 55. 13.101
  libavfilter     4.  8.100 /  4.  8.100
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ ./ffmpeg -threads 4 -i "input_sintel_trailer_720p.mp4" -profile:v baseline -level 4.0 -vf scale="trunc(360*a/2)*2:360, subtitles='subtitle.srt'"  -start_number 0 -hls_time 10 -hls_list_size 0 -f hls videos/4444/index_4444_360_.m3u8

but got an error

Could not write header for output file #0 (incorrect codec parameters ?): No such file or directory

Any help in this regard would be appreciated. I don't have much experience with compiling , otherwise would have done with support for libass as mentioned in the documentation here

console output after trying the first potential answer:

(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ ls
bbb_sunflower_1080p_30fps_normal.mp4  ffmpeg_log_multiprocess.txt  ffmpeg.static.64bit.latest  input_sintel_trailer_720p.mp4  watermark.png
encoding_script.sh                    ffmpeg_log_simple.txt        ffprobe                     subtitle.srt
ffmpeg                                ffmpeg_log.txt               fontconfig                  videos
(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ ffmpeg -threads 4 -i input_sintel_trailer_720p.mp4 -profile:v baseline -level 4.0 -vf "scale=-2:360,subtitles='subtitle.srt'" -start_number 0 -hls_time 10 -hls_list_size 0 -f hls videos/4444/index_4444_360_.m3u8
ffmpeg version N-78590-g5590ab4 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab
  libavutil      55. 18.100 / 55. 18.100
  libavcodec     57. 24.103 / 57. 24.103
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 32.100 /  6. 32.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input_sintel_trailer_720p.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    title           : Sintel Trailer
    artist          : Durian Open Movie Team
    encoder         : Lavf52.62.0
    copyright       : (c) copyright Blender Foundation | durian.blender.org
    description     : Trailer for the Sintel open movie project
  Duration: 00:00:52.21, start: 0.000000, bitrate: 1165 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 1033 kb/s, 24 fps, 24 tbr, 24 tbn, 48 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 126 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
[Parsed_subtitles_1 @ 0x2bdb700] Shaper: FriBidi 0.19.6 (SIMPLE) HarfBuzz-ng 0.9.27 (COMPLEX)
[Parsed_subtitles_1 @ 0x2bdb700] Using font provider fontconfig
[libx264 @ 0x2bc8b60] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x2bc8b60] profile Constrained Baseline, level 4.0
Output #0, hls, to 'videos/4444/index_4444_360_.m3u8':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    description     : Trailer for the Sintel open movie project
    title           : Sintel Trailer
    artist          : Durian Open Movie Team
    copyright       : (c) copyright Blender Foundation | durian.blender.org
    encoder         : Lavf57.25.100
    Stream #0:0(und): Video: h264 (libx264), yuv420p, 640x360, q=-1--1, 24 fps, 24 tbn, 24 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
      encoder         : Lavc57.24.103 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
      encoder         : Lavc57.24.103 aac
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Could not write header for output file #0 (incorrect codec parameters ?): No such file or directory
[aac @ 0x2bc9ac0] Qavg: -nan
(ffmpeg)aameer@falcon:~/Documents/projects/ffmpeg$ 

Solution

  • After some guidance from @Mulvya and a Mortiz from ffmpeg group I was finally able to get the solution which I was looking for which I am pasting for anyone who is looking for something similar. The ffmpeg I was using was fine(you can get ffmpeg I used here), there is a new static build which also works you can get it here as was pointed out by mortiz but somehow small mistakes in the commands which I was using were the issue, command which worked for me :

    ffmpeg -threads 4 -i input_sintel_trailer_720p.mp4 -profile:v baseline -level 4.0 -vf "scale=-2:360,subtitles=subtitle.ass:force_style='FontName=Aaargh/Aaargh.ttf,PrimaryColour=&HAA00FF00" -start_number 0 -hls_time 10 -hls_list_size 0 -f hls index_sub_ass_360_.m3u8
    

    Note that I am using .ass subtitle file for some styling of the subtitles. For anyone searching for that , here is the .ass file with two different styles for two different subtitles:

    [Script Info]
    ; Script generated by FFmpeg/Lavc57.24.103
    ScriptType: v4.00+
    PlayResX: 384
    PlayResY: 288
    
    [V4+ Styles]
    Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
    Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,5,10,10,10,0
    
    [Events]
    Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
    Dialogue: 0,0:00:10.00,0:00:10.04,Default,,0,0,0,,Senator, we're making\Nour final approach into Coruscant.
    Dialogue: 0,0:00:30.00,0:00:30.05,Default,,0,0,0,,Very good, Lieutenant.
    

    Moreover I am overriding the color and font and as mentioned by Mulvya that "scale=-2:360" part maintains the aspect ratio. Moreover I would like to reference some important links which helped me to reach to this point:

    ffmpeg subtitle docs here .ass format description here ffmpeg with threads here I also created a master list for different formats of hls videos if are interested in that look here my discussion with mortiz here for converting .srt to .ass subtitle file with ffmpeg check this Hope this helps.