Code Bug Fix: FFMPEG MKV to HLS how to keep audio and video in sync using codec_time_base

Original Source Link

I am doing BULK encodes of MKVs to multi resolution, audio track, subtitle track HLS folders. I have ran into a issue with Audio not being in sync. Locally or online. is there a way to include codec_time_base data into the encode to help with this?

Tagged : / / /

Making Game: Use vaapi with drawtext in FFmpeg

Original Source Link

I am trying to add the timestamp to the video.

I use drawtext with pts and gmtime to achieve it.

However, when I try to use the VAAPI to accelerate the progress, the error occur.

Here is my ffmpeg command:

ffmpeg 
  -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 
  -i input.mp4 
  -vf "drawtext=fontfile='/usr/share/fonts/truetype/msttcorefonts/arial.ttf': 
  [email protected]: fontsize=50: x=60: y=1000: 
  text='%{pts:gmtime:$(/bin/date -d "20200101 10:30:00" +"%s"):%Y-%m-%d %T}', 
  format=nv12|vaapi,hwupload,hwdownload" 
  -b:v 10M -c:a copy -vcodec h264_vaapi 
  -y output.mp4

It returns:

[hwupload @ 0x55d7727a4bc0] A hardware device reference is required to upload frames to.
[Parsed_hwupload_2 @ 0x55d7727a4ac0] Query format failed for 'Parsed_hwupload_2': Invalid argument
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!

If I remove the hwupload, I get:

Impossible to convert between the formats supported by the filter 'Parsed_format_1' and the filter 'auto_scaler_0'
Error reinitializing filters!
Failed to inject frame into filter network: Function not implemented
Error while processing the decoded data for stream #0:0
Conversion failed!

Could anyone help to point out what I am missing or wrong with the command?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Update:
@Gyan points out that I should insert the hwdownload before the drawtext. I follow his suggestion, and without hardware encoding, ffmpeg starts to work. I try to enable the hardware encoding, however, get the error again. Here is my modified command:

ffmpeg 
  -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 
  -i input.mp4 
  -vf "format=nv12|vaapi,hwdownload, 
  drawtext=fontfile='/usr/share/fonts/truetype/msttcorefonts/arial.ttf': 
  [email protected]: fontsize=50: x=60: y=1000: 
  text='%{pts:gmtime:$(/bin/date -d "20200101 10:30:00" +"%s"):%Y-%m-%d %T}', 
  format=nv12|vaapi,hwupload" 
  -b:v 10M -c:a copy -vcodec h264_vaapi 
  -y output.mp4

And the error message:

[hwupload @ 0x557b78829880] A hardware device reference is required to upload frames to.
[Parsed_hwupload_4 @ 0x557b78839500] Query format failed for 'Parsed_hwupload_4': Invalid argument
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!

Could anyone help to check the command again? Or, does the drawtext can not work with hardware encoding?

Tagged :

Server Bug Fix: Command FFMPEG works if runned from terminal but not from NGINX

Original Source Link

i have a code like this

 /home/ubuntu/bin/ffmpeg -re -v verbose -i rtmp://localhost:1935/live/title_event     
-http_persistent  1  -http_user_agent Custom-agent  -vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type event  -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename   http://my-server/low/title_event/360p_%03d.ts http://my-server/low/title_event/index.m3u8   
-http_persistent  1  -http_user_agent Custom-agent -vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type event -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename  http://my-server/mid/title_event/480p_%03d.ts  http://my-server/mid/title_event/index.m3u8  

This code is working fine if i start it from terminal, but if i put it inside an “exec” in my nginx conf…it not works.

If I run nginx service status I can watch for few seconds the command, but then command disappear and appears again after few seconds.

I already used, before this edit, FFMPEG from NGINX and it works.
I suppose problem can be “-http_persistent 1 -http_user_agent Custom-agent”, maybe nginx manage this kind of things in a different way. I don’t know :/

Tagged : / /

Linux HowTo: Set FFmpeg Process Priority with 2 Pass

Original Source Link

I’m using FFmpeg with Command Prompt.

I’m setting the process priority with start /low /b.


When pass 1 finishes, it launches FFmpeg again for pass 2 using &&.

start /low /b ffmpeg -i "C:video.mpg" -c:v libx264 -b:v 1500K -pass 1 "C:video.mp4" &&
start /low /b ffmpeg -i "C:video.mpg" -c:v libx264 -b:v 1500K -pass 2 "C:video.mp4"

Problem

Using start /low /b with pass 2 causes the error:

Failed to initialize encoder: Invalid parameter
Additional information: rc_twopass_stats_in.buf not set.

and pass 2 never starts.

Tagged : / /

Code Bug Fix: Blend video with ffmpeg instead of chromakey?

Original Source Link

I have a green background video. I want to separate the green background. With chromakey filter to bring results but I’m really not satisfied.I can replace it with blend. There is too little documentation for it. For example, color system and options.? Can anybody give me more suggestions about it. I appreciate it. thanks
Here is an example I collected.
-i “color=red:s=1280×720” -filter_complex “blend=shortest=1:all_mode=overlay:all_opacity=0.7”

Tagged : /

Making Game: ffmpeg: recommend baseline options on bad results

Original Source Link

I am running windows services to convert about 10k videos per year.
Most of videos make no problem, but there are some videos where I didnt see the problem:

Source Video

Video after conversion

=> The value of -b:v is extracted from FFProbe to get better results if the machine supports GPU encoding

Most videos are running on this samle cmd:

-i in.mp4 -an -vcodec libx264 -vprofile baseline -pix_fmt yuv420p -b:v 55600 -vf yadif -g 25 -movflags +faststart -y out.mp4

As far as I understand the problem: FFProbe returns only the avg bitrate wich is way to low.
I have tried to switch to -b:v, -maxrate:v, -bufsize:v with medium results. The videos are mutch better but still below the org file. even with the same bitrates like the org file.

Questions:

  1. Is there a better way to convert all videos to baseline?
  2. Is it still recommend to convert videos to baseline if its mandotory to support the majorety of devices (PC/Mac/Android/IPhone)?
  3. Any idea how to detect videos my conversion has ruined without human interaction?

Unfortunately the videos are confidential, but i have cut a sample and here are the org meta data. The bad result is below:

Video 
ID                             : 1
Format                         : AVC
Format/Info                    : Advanced Video Codec
Format profile                 : [email protected]
Format settings, CABAC         : Yes
Format settings, RefFrames     : 5 frames
Codec ID                       : avc1
Codec ID/Info                  : Advanced Video Coding
Duration                       : 19 min 2 s
Bit rate                       : 801 kb/s
Width                          : 1 920 pixels
Height                         : 1 080 pixels
Display aspect ratio           : 16:9
Frame rate mode                : Constant
Frame rate                     : 30.000 FPS
Color space                    : YUV
Chroma subsampling             : 4:2:0
Bit depth                      : 8 bits
Scan type                      : Progressive
Bits/(Pixel*Frame)             : 0.013
Stream size                    : 109 MiB (76%)
Writing library                : x264 core 160 r10 33f9e14
Encoding settings              : cabac=1 / ref=5 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=8 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=34 / lookahead_threads=5 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / stitchable=1 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=infinite / keyint_min=30 / scenecut=40 / intra_refresh=0 / rc_lookahead=50 / rc=crf / mbtree=1 / crf=20.0 / qcomp=0.60 / qpmin=5 / qpmax=69 / qpstep=4 / vbv_maxrate=5500 / vbv_bufsize=15000 / crf_max=0.0 / nal_hrd=none / filler=0 / ip_ratio=1.40 / aq=1:1.00
Encoded date                   : UTC 2020-05-07 15:14:22
Tagged date                    : UTC 2020-05-07 15:14:22
Color range                    : Limited
Color primaries                : BT.709
Transfer characteristics       : BT.709
Matrix coefficients            : BT.709

What exactly is the issue? Why are you trying to match the bitrate at all? Why not use crf encoding?

Is it still recommend to convert videos to baseline if its mandotory to support the majorety of devices (PC/Mac/Android/IPhone)?

The last iPhone that could not play high profile was released over 8 years ago. Unless you need support for an iPhone 4, No.

Any idea how to detect misguided videos without human interaction?

What is a “misguided” video?

Thx szatmary for pushing me into the right direction!

  1. Is there a better way to convert all videos to baseline?

    A: Yes, dont use baseline

  2. Is it still recommend to convert videos to baseline if its mandotory to support the majorety of devices (PC/Mac/Android/IPhone)?

    A: No! We will switch to the High Profile, and try crf for streaming (a lot of static content)

  3. Any idea how to detect videos my conversion has ruined without human interaction?

    A: search for ‘crf’ at the org. Encoding settings. Converting them to constant bitrate need other strategies

Tagged : / / /

Linux HowTo: ffmpeg inside nginx-rtmp module doesn’t work when scaling video

Original Source Link

So I setup an nginx RTMP server using this rtmp module. Below is my nginx.conf that I made following this guide.

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                        record off;
                }

                application movie {
                        live on;
                        record off;

                        exec ffmpeg -i rtmp://localhost:1935/movie -async 1 -vsync -1
                                -c:v libx264 -c:a libvo_aacenc -b:v 256k -b:a 32k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_low
                                -c:v libx264 -c:a libvo_aacenc -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_mid
                                -c:v libx264 -c:a libvo_aacenc -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_high
                                -c:v libx264 -c:a libvo_aacenc -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_hd720
                                -c copy -f flv rtmp://localhost:1935/hls/movie_src;
                }

                application hls {
                        live on;
                        hls on;
                        hls_path /tmp/hls;
                        hls_nested on;

                        hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
                        hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
                        hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution
                        hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution
                        hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
                }
        }
}

For some reason, scaling the video with -vf "scale=..." doesn’t work (same goes for using the -s wxh flag), as no output is created. I verify this by checking the stat page that the rtmp module provides. When I begin streaming to the server, the /movie application shows data streams, but the /hls application gets nothing. If I remove the -vf "scale=..." flags then everything works as expected with 5 streams under the /hls application. Am I doing something wrong? I would like to have scaling as I don’t want to be sending the same resolution across different bitrates. My version of nginx is 1.16.1, and this is all running on Ubuntu 18.04.

After running the ffmpeg command in the command line as per @BenXO’s suggestion, I got an error about libvo_aacenc not found, i.e. this snippet was the issue: -c:a libvo_aacenc. A quick search yielded that ffmpeg no longer supports this encoder and it’s recommended to just use aac. Changing all instances of -c:a libvo_aacenc to -c:a aac fixed the issue.

Tagged : / / /

Making Game: ffmpeg inside nginx-rtmp module doesn’t work when scaling video

Original Source Link

So I setup an nginx RTMP server using this rtmp module. Below is my nginx.conf that I made following this guide.

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                        record off;
                }

                application movie {
                        live on;
                        record off;

                        exec ffmpeg -i rtmp://localhost:1935/movie -async 1 -vsync -1
                                -c:v libx264 -c:a libvo_aacenc -b:v 256k -b:a 32k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_low
                                -c:v libx264 -c:a libvo_aacenc -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_mid
                                -c:v libx264 -c:a libvo_aacenc -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_high
                                -c:v libx264 -c:a libvo_aacenc -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/movie_hd720
                                -c copy -f flv rtmp://localhost:1935/hls/movie_src;
                }

                application hls {
                        live on;
                        hls on;
                        hls_path /tmp/hls;
                        hls_nested on;

                        hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
                        hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
                        hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution
                        hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution
                        hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
                }
        }
}

For some reason, scaling the video with -vf "scale=..." doesn’t work (same goes for using the -s wxh flag), as no output is created. I verify this by checking the stat page that the rtmp module provides. When I begin streaming to the server, the /movie application shows data streams, but the /hls application gets nothing. If I remove the -vf "scale=..." flags then everything works as expected with 5 streams under the /hls application. Am I doing something wrong? I would like to have scaling as I don’t want to be sending the same resolution across different bitrates. My version of nginx is 1.16.1, and this is all running on Ubuntu 18.04.

After running the ffmpeg command in the command line as per @BenXO’s suggestion, I got an error about libvo_aacenc not found, i.e. this snippet was the issue: -c:a libvo_aacenc. A quick search yielded that ffmpeg no longer supports this encoder and it’s recommended to just use aac. Changing all instances of -c:a libvo_aacenc to -c:a aac fixed the issue.

Tagged : / / /

Making Game: Set FFmpeg Process Priority with 2 Pass

Original Source Link

I’m using FFmpeg with Command Prompt.

I’m setting the process priority with start /low /b.


When pass 1 finishes, it launches FFmpeg again for pass 2 using &&.

start /low /b ffmpeg -i "C:video.mpg" -c:v libx264 -b:v 1500K -pass 1 "C:video.mp4" &&
start /low /b ffmpeg -i "C:video.mpg" -c:v libx264 -b:v 1500K -pass 2 "C:video.mp4"

Problem

Using start /low /b with pass 2 causes the error:

Failed to initialize encoder: Invalid parameter
Additional information: rc_twopass_stats_in.buf not set.

and pass 2 never starts.

Tagged : / /

Making Game: ffmpeg remove audio from video start and end time

Original Source Link

Something like this works I got this code from stack exchange

ffmpeg -i video -c:v copy -af volume=0:enable='between(t,60,100)' out.mp4<br>

i do not want to use the t60 method I want to rather use something like -ss 00:00:03 -t 00:00:08 but it chops the video up.
I want to just remove audio using 00:00:00

Tagged :