ffmpeg对mp4文件进行ts切片并生成m3u8文件

首先可以通过ffprobe查看视频信息 (可选)

执行: ./ffprobe Beach-Ball.mp4

iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static # ./ffprobe Beach-Ball.mp4 
ffprobe version 4.0.2-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2007-2018 the FFmpeg developers
 built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
 configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg
 libavutil 56. 14.100 / 56. 14.100
 libavcodec 58. 18.100 / 58. 18.100
 libavformat 58. 12.100 / 58. 12.100
 libavdevice 58. 3.100 / 58. 3.100
 libavfilter 7. 16.100 / 7. 16.100
 libswscale 5. 1.100 / 5. 1.100
 libswresample 3. 1.100 / 3. 1.100
 libpostproc 55. 1.100 / 55. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Beach-Ball.mp4':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 creation_time : 2015-08-24T11:27:32.000000Z
 encoder : Lavf55.33.100
 Duration: 00:00:15.70, start: 0.000000, bitrate: 2526 kb/s
 Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2525 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
 Metadata:
 creation_time : 2015-08-24T11:27:32.000000Z
 handler_name : VideoHandler
iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static #

再用ffmpeg把Beach-Ball.mp4文件转换为beachball.ts文件:

./ffmpeg -y -i Beach-Ball.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb beachball.ts

iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static # ./ffmpeg -y -i Beach-Ball.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb beachball.ts 
ffmpeg version 4.0.2-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2018 the FFmpeg developers
 built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
 configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg
 libavutil 56. 14.100 / 56. 14.100
 libavcodec 58. 18.100 / 58. 18.100
 libavformat 58. 12.100 / 58. 12.100
 libavdevice 58. 3.100 / 58. 3.100
 libavfilter 7. 16.100 / 7. 16.100
 libswscale 5. 1.100 / 5. 1.100
 libswresample 3. 1.100 / 3. 1.100
 libpostproc 55. 1.100 / 55. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Beach-Ball.mp4':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 creation_time : 2015-08-24T11:27:32.000000Z
 encoder : Lavf55.33.100
 Duration: 00:00:15.70, start: 0.000000, bitrate: 2526 kb/s
 Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2525 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
 Metadata:
 creation_time : 2015-08-24T11:27:32.000000Z
 handler_name : VideoHandler
Output #0, mpegts, to 'beachball.ts':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 encoder : Lavf58.12.100
 Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 2525 kb/s, 30 fps, 30 tbr, 90k tbn, 15360 tbc (default)
 Metadata:
 creation_time : 2015-08-24T11:27:32.000000Z
 handler_name : VideoHandler
Stream mapping:
 Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame= 471 fps=0.0 q=-1.0 Lsize= 5276kB time=00:00:15.66 bitrate=2758.8kbits/s speed=1.31e+03x 
video:4840kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 9.009212%
iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static # ll
total 201608
-rw-r--r-- 1 root root 4958325 Sep 22 2017 Beach-Ball.mp4
-rw-r--r-- 1 activemq 1000 35147 Jul 19 09:16 GPLv3.txt
-rw-r--r-- 1 root root 5402744 Sep 19 21:37 beachball.ts
-rwxr-xr-x 1 activemq 1000 64639464 Jul 19 08:56 ffmpeg
-rwxr-xr-x 1 activemq 1000 65855848 Jul 19 09:16 ffmpeg-10bit
-rwxr-xr-x 1 activemq 1000 64541160 Jul 19 08:56 ffprobe
drwxr-xr-x 2 activemq 1000 4096 Jul 19 09:00 manpages
drwxr-xr-x 2 activemq 1000 4096 Jul 19 07:35 model
-rwxr-xr-x 1 activemq 1000 742480 Jul 19 08:56 qt-faststart
-rw-r--r-- 1 activemq 1000 2958 Jul 19 09:16 readme.txt

最后用ffmpeg把beachball.ts文件切片并生成playlist.m3u8文件,5秒一个切片:

./ffmpeg -i beachball.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 5 beachball%03d.ts

iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static # ./ffmpeg -i beachball.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 5 beachball%03d.ts
ffmpeg version 4.0.2-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2018 the FFmpeg developers
 built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
 configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg
 libavutil 56. 14.100 / 56. 14.100
 libavcodec 58. 18.100 / 58. 18.100
 libavformat 58. 12.100 / 58. 12.100
 libavdevice 58. 3.100 / 58. 3.100
 libavfilter 7. 16.100 / 7. 16.100
 libswscale 5. 1.100 / 5. 1.100
 libswresample 3. 1.100 / 3. 1.100
 libpostproc 55. 1.100 / 55. 1.100
Input #0, mpegts, from 'beachball.ts':
 Duration: 00:00:15.70, start: 1.400000, bitrate: 2752 kb/s
 Program 1 
 Metadata:
 service_name : Service01
 service_provider: FFmpeg
 Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 30 fps, 30 tbr, 90k tbn, 60 tbc
[segment @ 0x4df3180] Opening 'beachball000.ts' for writing
Output #0, segment, to 'beachball%03d.ts':
 Metadata:
 encoder : Lavf58.12.100
 Stream #0:0: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 30 fps, 30 tbr, 90k tbn, 30 tbc
Stream mapping:
 Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[segment @ 0x4df3180] Opening 'playlist.m3u8.tmp' for writing
[segment @ 0x4df3180] Opening 'beachball001.ts' for writing
[segment @ 0x4df3180] Opening 'playlist.m3u8.tmp' for writing
[segment @ 0x4df3180] Opening 'beachball002.ts' for writing
[segment @ 0x4df3180] Opening 'playlist.m3u8.tmp' for writing
frame= 471 fps=0.0 q=-1.0 Lsize=N/A time=00:00:15.66 bitrate=N/A speed= 765x 
video:4843kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static # ll
total 206904
-rw-r--r-- 1 root root 4958325 Sep 22 2017 Beach-Ball.mp4
-rw-r--r-- 1 activemq 1000 35147 Jul 19 09:16 GPLv3.txt
-rw-r--r-- 1 root root 5402744 Sep 19 21:37 beachball.ts
-rw-r--r-- 1 root root 1740692 Sep 19 21:38 beachball000.ts
-rw-r--r-- 1 root root 1729036 Sep 19 21:38 beachball001.ts
-rw-r--r-- 1 root root 1933016 Sep 19 21:38 beachball002.ts
-rwxr-xr-x 1 activemq 1000 64639464 Jul 19 08:56 ffmpeg
-rwxr-xr-x 1 activemq 1000 65855848 Jul 19 09:16 ffmpeg-10bit
-rwxr-xr-x 1 activemq 1000 64541160 Jul 19 08:56 ffprobe
drwxr-xr-x 2 activemq 1000 4096 Jul 19 09:00 manpages
drwxr-xr-x 2 activemq 1000 4096 Jul 19 07:35 model
-rw-r--r-- 1 root root 213 Sep 19 21:38 playlist.m3u8
-rwxr-xr-x 1 activemq 1000 742480 Jul 19 08:56 qt-faststart
-rw-r--r-- 1 activemq 1000 2958 Jul 19 09:16 readme.txt
iZm5ecuy8n2epwa3gno33xZ:/opt/yizhichao/ffmpeg-4.0.2-64bit-static #

ffmpeg对mp4文件进行ts切片并生成m3u8文件

相关推荐