我正在制作音频/视频上传和流媒体项目。
处理:
我有2个音频文件和一个我用FFMPEG处理的视频文件。我需要以下面提到的方式处理这些音频和视频文件:
问题:
一切都工作正常,但问题是所有这一过程消耗非常高的CPU和TIME来完成。无论如何都要优化FFMPEG命令以快速运行。现在,它很慢!!
码:
我创建了一个bat文件来完成上面提到的过程。下面是通过nodejs服务器执行的批处理文件的源代码。
@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 ##########################################"
-threads
参数设置为1:如果您有4个内核,配置-threads 1
应该将FFMPEG进程限制为大约25%的CPU,这也将限制磁盘使用。使用3/4核心很多。您可以免费下载包含管理转换代码的开源Video Share VOD turnkey site plugin,包括在单个或不同进程中排队和生成多个输出的多个选项,添加水印,生成HLS段。