1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import * as ffmpeg from 'fluent-ffmpeg'
7 import { VideoDetails, VideoState } from '../../../../shared/models/videos'
8 import { getVideoFileFPS, audio } from '../../../helpers/ffmpeg-utils'
10 buildAbsoluteFixturePath,
12 flushAndRunMultipleServers,
19 setAccessTokensToServers,
23 import { join } from 'path'
24 import { waitJobs } from '../../utils/server/jobs'
26 const expect = chai.expect
28 describe('Test video transcoding', function () {
29 let servers: ServerInfo[] = []
31 before(async function () {
35 servers = await flushAndRunMultipleServers(2)
37 await setAccessTokensToServers(servers)
40 it('Should not transcode video on server 1', async function () {
43 const videoAttributes = {
44 name: 'my super name for server 1',
45 description: 'my super description for server 1',
46 fixture: 'video_short.webm'
48 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
50 await waitJobs(servers)
52 const res = await getVideosList(servers[0].url)
53 const video = res.body.data[0]
55 const res2 = await getVideo(servers[0].url, video.id)
56 const videoDetails = res2.body
57 expect(videoDetails.files).to.have.lengthOf(1)
59 const magnetUri = videoDetails.files[0].magnetUri
60 expect(magnetUri).to.match(/\.webm/)
62 const torrent = await webtorrentAdd(magnetUri)
63 expect(torrent.files).to.be.an('array')
64 expect(torrent.files.length).to.equal(1)
65 expect(torrent.files[0].path).match(/\.webm$/)
68 it('Should transcode video on server 2', async function () {
71 const videoAttributes = {
72 name: 'my super name for server 2',
73 description: 'my super description for server 2',
74 fixture: 'video_short.webm'
76 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
78 await waitJobs(servers)
80 const res = await getVideosList(servers[1].url)
82 const video = res.body.data[0]
83 const res2 = await getVideo(servers[1].url, video.id)
84 const videoDetails = res2.body
86 expect(videoDetails.files).to.have.lengthOf(4)
88 const magnetUri = videoDetails.files[0].magnetUri
89 expect(magnetUri).to.match(/\.mp4/)
91 const torrent = await webtorrentAdd(magnetUri)
92 expect(torrent.files).to.be.an('array')
93 expect(torrent.files.length).to.equal(1)
94 expect(torrent.files[0].path).match(/\.mp4$/)
97 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
100 const videoAttributes = {
102 fixture: 'video_short_mp3_256k.mp4'
104 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
106 await waitJobs(servers)
108 const res = await getVideosList(servers[1].url)
110 const video = res.body.data.find(v => v.name === videoAttributes.name)
111 const res2 = await getVideo(servers[1].url, video.id)
112 const videoDetails: VideoDetails = res2.body
114 expect(videoDetails.files).to.have.lengthOf(4)
116 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
117 const probe = await audio.get(ffmpeg, path)
119 if (probe.audioStream) {
120 expect(probe.audioStream['codec_name']).to.be.equal('aac')
121 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
123 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
127 it('Should transcode video with no audio and have no audio itself', async function () {
130 const videoAttributes = {
132 fixture: 'video_short_no_audio.mp4'
134 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
136 await waitJobs(servers)
138 const res = await getVideosList(servers[1].url)
140 const video = res.body.data.find(v => v.name === videoAttributes.name)
141 const res2 = await getVideo(servers[1].url, video.id)
142 const videoDetails: VideoDetails = res2.body
144 expect(videoDetails.files).to.have.lengthOf(4)
145 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
146 const probe = await audio.get(ffmpeg, path)
147 expect(probe).to.not.have.property('audioStream')
150 it('Should leave the audio untouched, but properly transcode the video', async function () {
153 const videoAttributes = {
154 name: 'untouched_audio',
155 fixture: 'video_short.mp4'
157 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
159 await waitJobs(servers)
161 const res = await getVideosList(servers[1].url)
163 const video = res.body.data.find(v => v.name === videoAttributes.name)
164 const res2 = await getVideo(servers[1].url, video.id)
165 const videoDetails: VideoDetails = res2.body
167 expect(videoDetails.files).to.have.lengthOf(4)
168 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
169 const fixtureVideoProbe = await audio.get(ffmpeg, fixturePath)
170 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
171 const videoProbe = await audio.get(ffmpeg, path)
172 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
173 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
174 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
176 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
180 it('Should transcode a 60 FPS video', async function () {
183 const videoAttributes = {
184 name: 'my super 30fps name for server 2',
185 description: 'my super 30fps description for server 2',
186 fixture: '60fps_720p_small.mp4'
188 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
190 await waitJobs(servers)
192 const res = await getVideosList(servers[1].url)
194 const video = res.body.data.find(v => v.name === videoAttributes.name)
195 const res2 = await getVideo(servers[1].url, video.id)
196 const videoDetails: VideoDetails = res2.body
198 expect(videoDetails.files).to.have.lengthOf(4)
199 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
200 expect(videoDetails.files[1].fps).to.be.below(31)
201 expect(videoDetails.files[2].fps).to.be.below(31)
202 expect(videoDetails.files[3].fps).to.be.below(31)
204 for (const resolution of [ '240', '360', '480' ]) {
205 const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
206 const fps = await getVideoFileFPS(path)
208 expect(fps).to.be.below(31)
211 const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
212 const fps = await getVideoFileFPS(path)
214 expect(fps).to.be.above(58).and.below(62)
217 it('Should wait transcoding before publishing the video', async function () {
220 await doubleFollow(servers[0], servers[1])
222 await waitJobs(servers)
225 // Upload the video, but wait transcoding
226 const videoAttributes = {
227 name: 'waiting video',
228 fixture: 'video_short1.webm',
229 waitTranscoding: true
231 const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
232 const videoId = resVideo.body.video.uuid
234 // Should be in transcode state
235 const { body } = await getVideo(servers[ 1 ].url, videoId)
236 expect(body.name).to.equal('waiting video')
237 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
238 expect(body.state.label).to.equal('To transcode')
239 expect(body.waitTranscoding).to.be.true
241 // Should have my video
242 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
243 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
244 expect(videoToFindInMine).not.to.be.undefined
245 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
246 expect(videoToFindInMine.state.label).to.equal('To transcode')
247 expect(videoToFindInMine.waitTranscoding).to.be.true
249 // Should not list this video
250 const resVideos = await getVideosList(servers[1].url)
251 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
252 expect(videoToFindInList).to.be.undefined
254 // Server 1 should not have the video yet
255 await getVideo(servers[0].url, videoId, 404)
258 await waitJobs(servers)
260 for (const server of servers) {
261 const res = await getVideosList(server.url)
262 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
263 expect(videoToFind).not.to.be.undefined
265 const res2 = await getVideo(server.url, videoToFind.id)
266 const videoDetails: VideoDetails = res2.body
268 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
269 expect(videoDetails.state.label).to.equal('Published')
270 expect(videoDetails.waitTranscoding).to.be.true
274 after(async function () {
275 killallServers(servers)