视频流使用FFmpeg将mp4视频转换为m3u8/ts文件

  风信建站新的开发项目中引用视频流技术,为了有效解决大视频在数据请求过程中加载过慢的问题,经过多次测试,决定使用m3u8格式做为视频流的格式,那么mp4和m3u8格式的视频各有什么缺点和优点呢?

   

  mp4视频格式有以下优点:

   

  1:跨平台支持,即支持pc端和移动端(ios和安卓)。

  2:该格式性能优越。

   

  mp4视频的缺点:

   

  1:视频时间长度过长时,在移动端会消耗大量的流量,但随着互联网流量资费的发展(将来流量是包月/年的,每月交一定费用,流量无限使用。)该缺点将不再存在。

  2:视频文件相对大一些。对于服务器的空间占用方面会有一定压力。

   

  视频本身的要求

  1:视频转码格式需要是h.264

  2:音频转码格式需要是AAC

  3:如果考虑到需要支持在PC端低版本IE(IE9及以下)的环境中(在该环境中只能使用flashplayer播放)支持视频请求未下载到用户本地缓存区的内容播放。则需要视频本身存在关键帧和关键时间点(服务器方面也有要求)。

   

  m3u8的优点:

   

  1:因为视频被切成多个.ts文件,所以不需要考虑元数据的问题,并且可以分段加载。

  2:在PC端使用flashplayer播放时可以实现视频的绝对加密。也就是视频即使是被下载到用户本地也无法使用。

   

  m3u8的缺点

   

  正常情况下不支持PC端的播放。ckplayer做了兼容是支持在pc端播放的,前提是pc端浏览器需要支持flashplayer

   

  使用m3u8我们需要将mp4视频进行转码,命令如下:

   

  ffmpeg -i 2.mp4 -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_list m3u8/2.m3u8 -segment_time 10 m3u8/out%03d.ts

   

  G:phpstudy_prowwwrootebiaoip-vpublic>ffmpeg -i 2.mp4 -codec copy -vbsf h264_

  mp4toannexb -map 0 -f segment -segment_list m3u8/2.m3u8 -segment_time 10 m3u8/ou

  t%03d.ts

  ffmpeg version n4.3.1-23-gc19641b2e2 Copyright (c) 2000-2020 the FFmpeg develope

  rs

    built with gcc 9.3-win32 (GCC) 20200320

    configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-conf

  ig=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw

  32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib -

  -enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-l

  zma --enable-fontconfig --enable-opencl --enable-libvmaf --disable-vulkan --enab

  le-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --

  enable-libdavs2 --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --ena

  ble-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libt

  heora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrn

  b --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-lib

  rubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --en

  able-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-l

  ibxavs2 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --e

  xtra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp

    libavutil      56. 51.100 / 56. 51.100

    libavcodec     58. 91.100 / 58. 91.100

    libavformat    58. 45.100 / 58. 45.100

    libavdevice    58. 10.100 / 58. 10.100

    libavfilter     7. 85.100 /  7. 85.100

    libswscale      5.  7.100 /  5.  7.100

    libswresample   3.  7.100 /  3.  7.100

    libpostproc    55.  7.100 / 55.  7.100

  Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':

    Metadata:

      major_brand     : isom

      minor_version   : 512

      compatible_brands: isomiso2avc1mp41

      encoder         : Lavf57.58.101

    Duration: 00:04:10.13, start: 0.000000, bitrate: 161 kb/s

      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x368 [

  SAR 1:1 DAR 40:23], 107 kb/s, 20 fps, 20 tbr, 10240 tbn, 80 tbc (default)

      Metadata:

        handler_name    : VideoHandler

      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, flt

  p, 48 kb/s (default)

      Metadata:

        handler_name    : SoundHandler

  [segment @ 00000000004b8d00] Opening 'm3u8/out000.ts' for writing

  Output #0, segment, to 'm3u8/out%03d.ts':

    Metadata:

      major_brand     : isom

      minor_version   : 512

      compatible_brands: isomiso2avc1mp41

      encoder         : Lavf58.45.100

      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x368 [

  SAR 1:1 DAR 40:23], q=2-31, 107 kb/s, 20 fps, 20 tbr, 90k tbn, 40 tbc (default)

      Metadata:

        handler_name    : VideoHandler

      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, flt

  p, 48 kb/s (default)

      Metadata:

        handler_name    : SoundHandler

  Stream mapping:

    Stream #0:0 -> #0:0 (copy)

    Stream #0:1 -> #0:1 (copy)

  Press [q] to stop, [?] for help

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out001.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out002.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out003.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out004.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out005.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out006.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out007.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out008.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out009.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out010.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out011.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out012.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out013.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out014.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out015.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out016.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out017.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out018.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/out019.ts' for writing

  [segment @ 00000000004b8d00] Opening 'm3u8/2.m3u8.tmp' for writing

  frame= 4996 fps=0.0 q=-1.0 Lsize=N/A time=00:04:10.09 bitrate=N/A speed=1.44e+03

  x

  video:3274kB audio:1468kB subtitle:0kB other streams:0kB global headers:0kB muxi

  ng overhead: unknown

   

  转码成功之后就可以在播放器中使用,加载速度快多啦!

  视频流使用FFmpeg将mp4视频转换为m3u8/ts文件

  视频流使用FFmpeg将mp4视频转换为m3u8/ts文件

   

  如需网站开发、微信开发,接口开发,建站功能定制开发,欢迎致电风信网络,电话:189-8648-0214(微信同号)