I Have written a go code but it is slow and the video compression rate is also not that impressive. I am new to FFMPEG and my entire project depends on FFMPEG. I have tried different video codecs like vp9, h264, h265, NVENC, AV1, etc. All of them were too slow (maybe I am not good enough to optimize it). My project is based on Go and the current codec that I am using is libx264. Can anyone help me optimize the video encoding part of my project.
Libx264:
func encodeVideo(fileName, bitrate, crf, preset, resolution string) *exec.Cmd {
return exec.Command("C:\\ffmpeg-6.1-full_build\\bin\\ffmpeg",
"-i", "./userUploadDatas/videos/"+fileName,
"-c:v", "libx264",
"-b:v", bitrate,
"-crf", crf,
"-preset", preset,
"-vf", "scale="+resolution,
"./userUploadDatas/videos/"+fileName+"_encoded"+".mp4")
}
Please provide static value of each parameters. Any codec will work for me as long as it is fast, occupies less space & doesn't loose spaces.
The problems I have faces with different codecs are:
- NVENC: Fast but the size of video is doubled & loss of video quality.
- libx264: Best I can find currently, but is slow.
- h264, h265: Occupies more space
- Av1 & vp9: Was too slow and wasn't able to encode 30sec video in 1hrs.
The specs of hardware that I am using is Ryzen7 5000 series CPU, NVIDIA RTX 3050 Ti Laptop GPU.
How much a video can be compressed while preserving quality depends highly on the target video's compression, codec, pixel format, etc. You can measure the bit rate against a video quality metric to determine if the transcoded videos meet your expectation.
As an example you can see in the graph a showcase of the relation: bit rate to quality for transcoding an H.264, YUV420, 8bit video to AV1 with av1_nvenc. The used script executed on Nvidia RTX 4090, Architecture: Ada Lovelace is:
-cq, my personal preference for this script is 30), otherwise the results are unpredictable-qminand-qmaxto the same value as-cq, but the file might get larger with little benefit for quality-rfor the original and distorted video, otherwise you may have a mismatch (frames that have a rating of 0), therefore you'll get way worse results than they actually are