FFmpeg: Get better encoding out of my function - windows

I needed some assistance on my task.
I am using FFmpeg to burn time and the channel name onto the video.
My goal is to record a stream that is html5 compatible with the following settings:
Video wrapper MP4
Video codec H.264
Bitrate 1Mbps
Audio codec AAC
Audio bitrate 128Kbps
And GPU encoding.
This is what I am using:
ffmpeg -hwaccel cuvid -y -i {udp} -vf "drawtext=fontfile=calibrib.tff:fontsize=25:text='{ChannelName} %{localtime}': x=10: y=10: fontcolor=white: box=1: boxcolor=0x000000" -pix_fmt yuv420p -vsync 1 -c:v h264_nvenc -r 25 -threads 0 -b:v 1M -profile:v main -minrate 1M -maxrate 1M -bufsize 10M -sc_threshold 0 -c:a aac -b:a 128k -ac 2 -ar 44100 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -bsf:v h264_mp4toannexb -t 00:30:00 {output}\{ChannelName}\{ChannelName}_{year}_{monthno}_{day}__{Hours}_{Minutes}_{Seconds}.mp4
{ChannelName}_{year}_{monthno}_{day}__{Hours}_{Minutes}_{Seconds} are all variable holding information.
{udp} holds the UDP stream link.
I have done it this way as I have multiple UDP stream recording.
Although this works, is there a better way for me to do this keeping in the -vf as I need the time and channel name.
Currently, this uses between 0.8% to 1.9% GPU on my Quadro P4000. I don't want to use more than this as I have more than 30 streams.

Here are some of the suggestion
-profile:v use Constrained Baseline Profile or Baseline Profile - as most of the Browser or HTML will support.
Check How many parallel instances of the Encoder you can run on GPU - Quadro P4000, remaining you can run on cpu.
Based on the resolution & fps you can decide the video bitrate of encoding range min & max bitrate. (-b:v 1M -minrate 1M -maxrate 1M) - refer : https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
-sc_threshold (FFmpeg)
Adjusts the sensitivity of x264's scenecut detection. Rarely needs to be adjusted. Recommended default: 40

Related

Live stream prerecorded video to YouTube using FFMPEG with 4500 kbps bitrate

I tried various ways to make a live streaming script with a bit rate according to YouTube's recommendation of 4500 Kbps bit rate.
The code:
ffmpeg -re -stream_loop -1 -i live1.mp4 -c copy -preset veryfast -b:v 7000k -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv -flvflags no_duration_filesize rtmp://a.rtmp.youtube.com/live2/(streamkey)
and in my current code, there is an error when live: Use a keyframe frequency of four seconds or less. Currently, keyframes are sent infrequently which can cause buffering. The current keyframe frequency is 5.0 seconds. Please note that errors in the transfer process can cause the size of the GOP (group of images) to be incorrect.
How to fix my code?
I've tried several ways, but the bit rate is still high, and the error is in YouTube Studio

How to use constrained/capped bit rate mode in FFmpeg

I used FFmpeg with this command to convert a video:
ffmpeg -i input -c:v libx264 -x264-params opencl=true -preset veryslow -crf 19 -maxrate 7000k -profile:v high -level 4.0 -bf 2 -coder 1 -pix_fmt yuv420p -c:a copy output.mkv
Note that I set -maxrate 7000k to set the maximum bit rate for the video. Still, the end result video has a maximum bit rate of 38.0 Mb/s.
Here are the results before (to the left) and after (to the right), if that helps.
Am I using maxrate properly? Should not the video be capped at 7000 kb/s?
maxrate is enforced via the bufsize option. No bufsize, no enforcement. See https://stackoverflow.com/a/33612662
The smaller the bufsize relative to maxrate, closer the actual peak bitrate to maxrate, but expect transient quality dips (if maxrate is too low for desired quality).
Start with
-maxrate 7000k -bufsize 7000k

ffmpeg process has GPU usage limit

i'm using ffmpeg and an NVIDIA for my video transcode process.
so i have one problem.
look at below image :
one process just use 263MiB of my second GPU. not completly use that !!
that is not good. i think there should be a way to remove this limitation for gpu process.
my ffmpeg command that i run is:
ffmpeg -y -loglevel info -hwaccel cuda -hwaccel_output_format cuda -hwaccel_device 1 -i "MYVIDEO" -vf scale_npp=w=426:h=240 -c:v h264_nvenc -profile:v main -b:v 400k -sc_threshold 0 -g 25 -keyint_min 25 -bf 2 -c:a aac -b:a 64k -ar 48000 -f hls -hls_time 6 -hls_playlist_type vod -hls_allow_cache 1 -hls_segment_filename f-0-seg-%d.ts f-0.m3u8
There is no limitation going on here, At least not related to memory.
You are scaling the video to 426x240 Assuming 4:2:0 subsampling, That is 153K per frame. The encoder needs 16 frames at most. Which is a little over 2MB. The GPU is using over 100 times that.

How to change audio frame rate in FFMPEG?

I am trying to change audio frame from 43.066 FPS (1024 SPF) to 23.438 FPS (2048 SPF) but no luck.
code tried :
ffmpeg -i 1.mp4 -b:v 2000k -vcodec libx264 -x265-params keyint=50:scenecut=0 -preset fast -pix_fmt yuv420p -profile:v main -level 3.1 -r 25 -s:v 1280x720 -ac 2 -c:a aac -b:a 128k -ar 48000 -aframes 23.438 HD2_500_500.mp4
Error :
Expected int64 for frames:a
You can change the audio speed using the ​atempo audio filter. The filter accepts exactly one parameter, the audio tempo. If not specified then the filter will assume nominal 1.0 tempo. Tempo must be in the [0.5, 100.0] range.
Here a simple example on how to speed down the audio by half:
ffmpeg -i 1.mp4 -filter:a "atempo=0.5" -vn HD2_500_500.mp4

encoding jpeg as h264 video

I am using the following command to encode an AVI to an H264 video for use in an HTML5 video tag:
ffmpeg -y -i "test.avi" -vcodec libx264 -vpre slow -vpre baseline -g 30 "out.mp4"
And this works just fine. But I also want to create a placeholder video (long story) from a single still image, so I do this:
ffmpeg -y -i "test.jpg" -vcodec libx264 -vpre slow -vpre baseline -g 30 "out.mp4"
And this doesn't work. What gives?
EDIT: After trying LordNeckbeards answer, here is my full output: http://pastebin.com/axhKpkLx
Example for a 10 second output:
ffmpeg -loop 1 -framerate 24 -i input.jpg -c:v libx264 -preset slow -tune stillimage -crf 24 -vf format=yuv420p -t 10 -movflags +faststart output.mp4
Same thing but with audio. The output duration will match the input audio duration:
ffmpeg -loop 1 -framerate 24 -i input.jpg -i audio.mp3 -c:v libx264 -preset slow -tune stillimage -crf 24 -vf format=yuv420p -c:a aac -shortest -movflags +faststart output.mp4
-loop 1 loops the image input.
-framerate sets the frame rate of the image input. Default is 25. Some players have issues with low frame rates so a value over 6 or so is recommended.
-i input.jpg the input.
-c:v libx264 the H.264 video encoder.
-preset x264 encoding preset. Use the slowest one you can.
-tune x264 tuning for various adjustments to fit specific situations.
-crf for quality. A lower value results in higher quality. Use the highest value that still provides an acceptable quality to you. Default is 23.
-vf format=yuv420p outputs the pixel format as yuv420p. This ensures the output uses a widely acceptable chroma sub-sampling scheme. Recommended for libx264 when encoding from images.
-c:a aac the AAC audio encoder. If your input is already AAC or M4A then use -c:a copy instead to stream copy instead of re-encode.
-t 10 (in the first example) makes a 10 second output. Needed because the image is looping indefinitely.
-shortest (in the second example) makes the output the same duration as the shortest input. In this case it is the audio since the image is looping indefinitely.
-movflags +faststart relocates the moov atom to the beginning of the file after encoding is finished. Allows playback to begin faster in progressive download playing; otherwise the whole video must be downloaded before playing.
-profile:v main (optional) some devices can't handle High profile.
See FFmpeg Wiki: H.264 for more info.

Resources