I am working on a audio/video uploading and streaming project.
Process:
I have 2 audio files and a video file which I am processing with FFMPEG. I need to process these audio and video files in the below mentioned way:
Generating thumbnail from the video file.
Merging audio files with each other.
then, merging the mixed audio file with the video file.
after that, adding watermark over the video file.
at last, splits video file into small chunks for streaming
ISSUE:
Everything is working fine but the issue is that all this process consuming very high CPU and TIME to complete. Is there anyway to optimize the FFMPEG commands to run in fastly. Right now, it is very slow!!
CODE:
I have created a bat file to complete the above mentioned process. Below is the source of the batch file which is executing over nodejs server.
@echo off
echo "########################################### Uploading Video #############################################"
:GETOPTS
if /I %~1 == --app-path set APP_PATH=%2& shift
if /I %~1 == --video-name set VIDEO_NAME=%2& shift
if /I %~1 == --do-merge set IS_DO_MERGE=%2& shift
if /I %~1 == --audio-name set AUDIO_NAME=%2& shift
if /I %~1 == --bg-beat-name set BEAT_NAME=%2& shift
shift
if not (%1)==() goto GETOPTS
set USER_DIR=%APP_PATH%data\content\userContent\
set BEAT_FILE_URI=%APP_PATH%data\content\beats\%BEAT_NAME%
set FFMPEG=%APP_PATH%data\cmd\ffmpeg\bin\ffmpeg.exe
set VIDEO_PATH=%USER_DIR%%VIDEO_NAME%
set MERGED_AUDIO_PATH=%VIDEO_PATH%_mix_audio.aac
set RESULTANT_PATH=%VIDEO_PATH%_finalized.mp4
set WATER_MARKED_PATH=%VIDEO_PATH%_finalized_watermark.mp4
echo "Taking screenshot - %VIDEO_PATH%"
%FFMPEG% -threads 3 -loglevel panic -y -ss 00:00:01 -i %VIDEO_PATH% -vframes 1 -filter:v scale="500:-1" %VIDEO_PATH%_screenshot.jpg
if "%IS_DO_MERGE%" == "true" (
echo "Combining audio with music file"
echo "OUTPUT: %MERGED_AUDIO_PATH%"
%FFMPEG% -threads 3 -loglevel panic -y -i %USER_DIR%%AUDIO_NAME% -i %BEAT_FILE_URI% -filter_complex amix=inputs=2:duration=shortest:dropout_transition=3 -shortest %MERGED_AUDIO_PATH%
echo "Merging Audio & video"
%FFMPEG% -threads 3 -loglevel panic -y -i %VIDEO_PATH% -i %MERGED_AUDIO_PATH% -c:v libx264 -preset ultrafast -crf 22 -tune zerolatency -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 -shortest %RESULTANT_PATH%
echo "Adding watermark"
%FFMPEG% -threads 3 -loglevel panic -y -i %RESULTANT_PATH% -i %APP_PATH%data\assets\watermark.png -filter_complex "overlay=x=(main_w-overlay_w)/2:y=30" -movflags +faststart %WATER_MARKED_PATH%
#ffmpeg -i input.mp4 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls index.m3u8
#%FFMPEG% -threads 3 -loglevel panic -y -i %WATER_MARKED_PATH% -i %APP_PATH%data\assets\watermark.png -filter_complex "overlay=x=(main_w-overlay_w)/2:y=30" %WATER_MARKED_PATH%
echo "--------------------------------- process completed with no errors ----------------------------------"
) else (
echo "No need to merge as --do-merge is false"
echo "Adding watermark"
%FFMPEG% -threads 3 -loglevel panic -y -i %VIDEO_PATH% -i %APP_PATH%data\assets\watermark.png -filter_complex "overlay=x=(main_w-overlay_w)/2:y=30" %WATER_MARKED_PATH%
)
echo "###################################### Uploading Video Ended ##########################################"
Encoding videos takes lots of processing. On a multi purpose server it's important to configure conversions to not affect server performance, even if that means running conversions longer in background.
You can set the -threads
parameter down to to 1: if you have 4 cores, configuring -threads 1
should limit FFMPEG process to around 25% CPU and that will also limit disk usage. Using 3/4 cores is a lot.
Latest CPU have 2 threads per core, resulting in 8 threads on 4 cores.
On a multiple-core computer, the threads command controls how many threads FFmpeg consumes.
For more details and experiments on threads parameter, see https://streaminglearningcenter.com/blogs/ffmpeg-command-threads-how-it-affects-quality-and-performance.html .
Conversions should be queued (run another only after previous completes). Running multiple conversions at same time can greatly impact disk performance and that can become an overall performance bottleneck.
You can do multiple operations in single process (read inputs once - audio, video, watermark and generate multiple outputs at same time) to reduce overall processing time (but that involves higher memory requirements).
Download for free the open source Video Share VOD turnkey site plugin that includes code to manage conversions, including multiple options to queue and generate multiple outputs in single or different processes, add watermark, generate HLS segments.