batch-fileffmpegvideo-streamingwindows-hostingvod

Optimize script commands FFMPEG with Limit CPU usage


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:

  1. Generating thumbnail from the video file.

  2. Merging audio files with each other.

  3. then, merging the mixed audio file with the video file.

  4. after that, adding watermark over the video file.

  5. 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  ##########################################"

Solution