依赖库 Link to heading

demo Link to heading

import { app } from 'electron'
const ffmpegPath = require('@ffmpeg-installer/ffmpeg')
const ffprobePath = require('@ffprobe-installer/ffprobe')
const ffmpeg = require('fluent-ffmpeg')

// asar打包后路径有所变化
if (process.env.NODE_ENV !== 'development') {
  ffmpeg.setFfmpegPath(ffmpegPath.path.replace('app.asar', 'app.asar.unpacked'))
  ffmpeg.setFfprobePath(ffprobePath.path.replace('app.asar', 'app.asar.unpacked'))
} else {
  ffmpeg.setFfmpegPath(ffmpegPath.path)
  ffmpeg.setFfprobePath(ffprobePath.path)
}
// rtsp/rtmp直播地址转m3u8例子
export default class VideoServer {
  constructor (video) {
    this.liveUrl = video.liveUrl;
    this.m3u8Path = video.m3u8Path;
    this.ffmpegCommand;
  }

  killFfmpegCommand () {
    if (this.ffmpegCommand) {
      this.ffmpegCommand.kill()
    }
  }

  startTransCode (fun) {
    this.ffmpegCommand = ffmpeg(this.liveUrl)
      .outputOptions([
        '-fflags flush_packets',
        '-max_delay 1',
        '-an',
        '-flags',
        '-global_header',
        '-hls_time 1',
        '-hls_list_size 3',
        '-hls_wrap 3',
        '-vcodec copy'
      ])
      .on('start', function(e) {
        console.log('---开始转码---')
        console.log(e);
        fun('info', e)
      })
      .on('end', function() {
        console.log('file has been converted succesfully');
      })
      .on('error', function(err) {
        console.log('an error happened: ' + err.message);
        fun('error')
      })
      .save(this.m3u8Path)
  }
}