Duke Yin's Technology database

使用ffmpge将视频转为HLS播放列表

安装ffmpeg

https://github.com/BtbN/FFmpeg-Builds/releases

或者

https://www.gyan.dev/ffmpeg/builds/

Windows最新版:

https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full.7z

设置Windows系统变量

Environment Variables -> User variables for USERNAME -> path -> new( or edit on windows7)

添加mmpeg安装路径 例如:

c:/mmpeg/bin/

建立目录结构

目标结构

  • videos
    • media
      • video1
        • hls
      • video2
        • hls
    • video1
    • video2
    • video3

定位到Video文件夹之后,新建一个同视频名称的目录

md media\some_fun_video_name\hls

使用mmpeg命令生成多种尺寸码率视频及m3u8列表

ffmpeg -i some_fun_video_name.mp4 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls ./media/some_fun_video_name/hls/360_out.m3u8
ffmpeg -i some_fun_video_name.mp4 -profile:v baseline -level 3.0 -s 800x480 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls ./media/some_fun_video_name/hls/480_out.m3u8
ffmpeg -i some_fun_video_name.mp4 -profile:v baseline -level 3.0 -s 1280x720 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls ./media/some_fun_video_name/hls/720_out.m3u8
ffmpeg -i some_fun_video_name.mp4 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls ./media/some_fun_video_name/hls/1080_out.m3u8
ffmpeg -i 输入文件名.mp4 -profile:v baseline -level 3.0 -s 长x宽 -start_number 0 -hls_time 切割单文件时长秒 -hls_list_size 0 -f hls 输出路径/输出文件名.m3u8

然后在转换好的目录 hls 下,新建一个文件作为主播放列表

some_fun_video_name.m3u8

内容如下

#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=375000,RESOLUTION=640x360
360_out.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=750000,RESOLUTION=854x480
480_out.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2000000,RESOLUTION=1280x720
720_out.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3500000,RESOLUTION=1920x1080
1080_out.m3u8

批处理

手动输入比较麻烦,可以使用批处理对单个视频进行转换。

将下面的代码保存为一个.bat文件,在windows中将想要处理的文件拖放到这个bat里,就会自动执行。

前提是,视频文件必须是mp4 h264格式视频,且视频文件bat文件必须在同一个目录中。

@echo off
echo The filename: %~n1
echo The extention: %~x1
REM Make new directories in the same folder.
md media\%~n1\hls
REM Get the cover jpg and preview gif from video
ffmpeg -i %~n1%~x1  -ss 20.00 -vframes 1 ./media/%~n1/hls/cover_h1.jpg
ffmpeg -i %~n1%~x1  -ss 20.00 -vframes 1 -s 760x428 ./media/%~n1/hls/cover_l1.jpg
ffmpeg -ss 00:00:10 -t 3 -i %~n1%~x1 -s 640x360 -r  2 ./media/%~n1/hls/preview.gif
REM Start to work on videos
ffmpeg -i %~n1%~x1 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -threads 5 -preset ultrafast -f hls ./media/%~n1/hls/360_out.m3u8
ffmpeg -i %~n1%~x1 -profile:v baseline -level 3.0 -s 800x480 -start_number 0 -hls_time 10 -hls_list_size 0 -threads 5 -preset ultrafast -f hls ./media/%~n1/hls/480_out.m3u8
ffmpeg -i %~n1%~x1 -profile:v baseline -level 3.0 -s 1280x720 -start_number 0 -hls_time 10 -hls_list_size 0 -threads 5 -preset ultrafast -f hls ./media/%~n1/hls/720_out.m3u8
ffmpeg -i %~n1%~x1 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -hls_time 10 -hls_list_size 0 -threads 5 -preset ultrafast -f hls ./media/%~n1/hls/1080_out.m3u8
REM Check if any error
if %ERRORLEVEL% == 0 goto :next
echo "Errors encountered during execution. Exited with status: %errorlevel%"
goto :endofscript
:next
echo "Generating overall m3u8 play list...."
REM Generate an overall play list.
(
echo #EXTM3U
echo #EXT-X-STREAM-INF:BANDWIDTH=375000,RESOLUTION=640x360
echo 360_out.m3u8
echo #EXT-X-STREAM-INF:BANDWIDTH=750000,RESOLUTION=854x480
echo 480_out.m3u8
echo #EXT-X-STREAM-INF:BANDWIDTH=2000000,RESOLUTION=1280x720
echo 720_out.m3u8
echo #EXT-X-STREAM-INF:BANDWIDTH=3500000,RESOLUTION=1920x1080
echo 1080_out.m3u8
) > ./media/%~n1/hls/%~n1.m3u8
echo "Overall m3u8 play list generated."
goto :endofscript
:endofscript
echo "Work complete."
pause

# #

引用关闭,但你能发布评论.

评论

标注 * 的为必填项。