electron中使用ffmpeg

依赖库

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
}
}