1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
3 import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
4 import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
5 import { getVideoWithToken } from './videos'
6 import { omit } from 'lodash'
8 function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
9 const path = '/api/v1/videos/live'
11 return makeGetRequest({
14 path: path + '/' + videoId,
19 function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
20 const path = '/api/v1/videos/live'
22 return makePutBodyRequest({
25 path: path + '/' + videoId,
31 function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
32 const path = '/api/v1/videos/live'
34 const attaches: any = {}
35 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
36 if (fields.previewfile) attaches.previewfile = fields.previewfile
38 const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
40 return makeUploadRequest({
45 fields: updatedFields,
50 function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
51 const fixture = buildAbsoluteFixturePath('video_short.mp4')
53 const command = ffmpeg(fixture)
54 command.inputOption('-stream_loop -1')
55 command.inputOption('-re')
57 command.outputOption('-c copy')
58 command.outputOption('-f flv')
60 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
61 command.output(rtmpUrl)
63 command.on('error', err => {
64 if (err?.message?.includes('Exiting normally')) return
66 console.error('Cannot send RTMP stream.', { err })
69 if (process.env.DEBUG) {
70 command.on('stderr', data => console.log(data))
78 async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
79 command.kill('SIGINT')
84 async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
85 let video: VideoDetails
88 const res = await getVideoWithToken(url, token, videoId)
92 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
95 // ---------------------------------------------------------------------------