fixed formatting, added test case
[oweals/peertube.git] / server / tests / api / videos / video-transcoder.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { omit } from 'lodash'
6 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
7 import { audio, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, canDoQuickTranscode } from '../../../helpers/ffmpeg-utils'
8 import {
9   buildAbsoluteFixturePath, cleanupTests,
10   doubleFollow,
11   flushAndRunMultipleServers,
12   generateHighBitrateVideo,
13   getMyVideos,
14   getVideo,
15   getVideosList,
16   waitJobs,
17   root,
18   ServerInfo,
19   setAccessTokensToServers,
20   uploadVideo,
21   webtorrentAdd
22 } from '../../../../shared/extra-utils'
23 import { join } from 'path'
24 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
25
26 const expect = chai.expect
27
28 describe('Test video transcoding', function () {
29   let servers: ServerInfo[] = []
30
31   before(async function () {
32     this.timeout(30000)
33
34     // Run servers
35     servers = await flushAndRunMultipleServers(2)
36
37     await setAccessTokensToServers(servers)
38
39     await doubleFollow(servers[0], servers[1])
40   })
41
42   it('Should not transcode video on server 1', async function () {
43     this.timeout(60000)
44
45     const videoAttributes = {
46       name: 'my super name for server 1',
47       description: 'my super description for server 1',
48       fixture: 'video_short.webm'
49     }
50     await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
51
52     await waitJobs(servers)
53
54     for (const server of servers) {
55       const res = await getVideosList(server.url)
56       const video = res.body.data[ 0 ]
57
58       const res2 = await getVideo(server.url, video.id)
59       const videoDetails = res2.body
60       expect(videoDetails.files).to.have.lengthOf(1)
61
62       const magnetUri = videoDetails.files[ 0 ].magnetUri
63       expect(magnetUri).to.match(/\.webm/)
64
65       const torrent = await webtorrentAdd(magnetUri, true)
66       expect(torrent.files).to.be.an('array')
67       expect(torrent.files.length).to.equal(1)
68       expect(torrent.files[ 0 ].path).match(/\.webm$/)
69     }
70   })
71
72   it('Should transcode video on server 2', async function () {
73     this.timeout(60000)
74
75     const videoAttributes = {
76       name: 'my super name for server 2',
77       description: 'my super description for server 2',
78       fixture: 'video_short.webm'
79     }
80     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
81
82     await waitJobs(servers)
83
84     for (const server of servers) {
85       const res = await getVideosList(server.url)
86
87       const video = res.body.data.find(v => v.name === videoAttributes.name)
88       const res2 = await getVideo(server.url, video.id)
89       const videoDetails = res2.body
90
91       expect(videoDetails.files).to.have.lengthOf(4)
92
93       const magnetUri = videoDetails.files[ 0 ].magnetUri
94       expect(magnetUri).to.match(/\.mp4/)
95
96       const torrent = await webtorrentAdd(magnetUri, true)
97       expect(torrent.files).to.be.an('array')
98       expect(torrent.files.length).to.equal(1)
99       expect(torrent.files[ 0 ].path).match(/\.mp4$/)
100     }
101   })
102
103   it('Should transcode high bit rate mp3 to proper bit rate', async function () {
104     this.timeout(60000)
105
106     const videoAttributes = {
107       name: 'mp3_256k',
108       fixture: 'video_short_mp3_256k.mp4'
109     }
110     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
111
112     await waitJobs(servers)
113
114     for (const server of servers) {
115       const res = await getVideosList(server.url)
116
117       const video = res.body.data.find(v => v.name === videoAttributes.name)
118       const res2 = await getVideo(server.url, video.id)
119       const videoDetails: VideoDetails = res2.body
120
121       expect(videoDetails.files).to.have.lengthOf(4)
122
123       const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
124       const probe = await audio.get(path)
125
126       if (probe.audioStream) {
127         expect(probe.audioStream[ 'codec_name' ]).to.be.equal('aac')
128         expect(probe.audioStream[ 'bit_rate' ]).to.be.at.most(384 * 8000)
129       } else {
130         this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
131       }
132     }
133   })
134
135   it('Should transcode video with no audio and have no audio itself', async function () {
136     this.timeout(60000)
137
138     const videoAttributes = {
139       name: 'no_audio',
140       fixture: 'video_short_no_audio.mp4'
141     }
142     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
143
144     await waitJobs(servers)
145
146     for (const server of servers) {
147       const res = await getVideosList(server.url)
148
149       const video = res.body.data.find(v => v.name === videoAttributes.name)
150       const res2 = await getVideo(server.url, video.id)
151       const videoDetails: VideoDetails = res2.body
152
153       expect(videoDetails.files).to.have.lengthOf(4)
154       const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
155       const probe = await audio.get(path)
156       expect(probe).to.not.have.property('audioStream')
157     }
158   })
159
160   it('Should leave the audio untouched, but properly transcode the video', async function () {
161     this.timeout(60000)
162
163     const videoAttributes = {
164       name: 'untouched_audio',
165       fixture: 'video_short.mp4'
166     }
167     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
168
169     await waitJobs(servers)
170
171     for (const server of servers) {
172       const res = await getVideosList(server.url)
173
174       const video = res.body.data.find(v => v.name === videoAttributes.name)
175       const res2 = await getVideo(server.url, video.id)
176       const videoDetails: VideoDetails = res2.body
177
178       expect(videoDetails.files).to.have.lengthOf(4)
179       const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
180       const fixtureVideoProbe = await audio.get(fixturePath)
181       const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
182       const videoProbe = await audio.get(path)
183       if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
184         const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
185         expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
186       } else {
187         this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
188       }
189     }
190   })
191
192   it('Should transcode a 60 FPS video', async function () {
193     this.timeout(60000)
194
195     const videoAttributes = {
196       name: 'my super 30fps name for server 2',
197       description: 'my super 30fps description for server 2',
198       fixture: '60fps_720p_small.mp4'
199     }
200     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
201
202     await waitJobs(servers)
203
204     for (const server of servers) {
205       const res = await getVideosList(server.url)
206
207       const video = res.body.data.find(v => v.name === videoAttributes.name)
208       const res2 = await getVideo(server.url, video.id)
209       const videoDetails: VideoDetails = res2.body
210
211       expect(videoDetails.files).to.have.lengthOf(4)
212       expect(videoDetails.files[ 0 ].fps).to.be.above(58).and.below(62)
213       expect(videoDetails.files[ 1 ].fps).to.be.below(31)
214       expect(videoDetails.files[ 2 ].fps).to.be.below(31)
215       expect(videoDetails.files[ 3 ].fps).to.be.below(31)
216
217       for (const resolution of [ '240', '360', '480' ]) {
218         const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
219         const fps = await getVideoFileFPS(path)
220
221         expect(fps).to.be.below(31)
222       }
223
224       const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
225       const fps = await getVideoFileFPS(path)
226
227       expect(fps).to.be.above(58).and.below(62)
228     }
229   })
230
231   it('Should wait for transcoding before publishing the video', async function () {
232     this.timeout(80000)
233
234     {
235       // Upload the video, but wait transcoding
236       const videoAttributes = {
237         name: 'waiting video',
238         fixture: 'video_short1.webm',
239         waitTranscoding: true
240       }
241       const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
242       const videoId = resVideo.body.video.uuid
243
244       // Should be in transcode state
245       const { body } = await getVideo(servers[ 1 ].url, videoId)
246       expect(body.name).to.equal('waiting video')
247       expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
248       expect(body.state.label).to.equal('To transcode')
249       expect(body.waitTranscoding).to.be.true
250
251       // Should have my video
252       const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
253       const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
254       expect(videoToFindInMine).not.to.be.undefined
255       expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
256       expect(videoToFindInMine.state.label).to.equal('To transcode')
257       expect(videoToFindInMine.waitTranscoding).to.be.true
258
259       // Should not list this video
260       const resVideos = await getVideosList(servers[1].url)
261       const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
262       expect(videoToFindInList).to.be.undefined
263
264       // Server 1 should not have the video yet
265       await getVideo(servers[0].url, videoId, 404)
266     }
267
268     await waitJobs(servers)
269
270     for (const server of servers) {
271       const res = await getVideosList(server.url)
272       const videoToFind = res.body.data.find(v => v.name === 'waiting video')
273       expect(videoToFind).not.to.be.undefined
274
275       const res2 = await getVideo(server.url, videoToFind.id)
276       const videoDetails: VideoDetails = res2.body
277
278       expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
279       expect(videoDetails.state.label).to.equal('Published')
280       expect(videoDetails.waitTranscoding).to.be.true
281     }
282   })
283
284   it('Should respect maximum bitrate values', async function () {
285     this.timeout(160000)
286
287     let tempFixturePath: string
288
289     {
290       tempFixturePath = await generateHighBitrateVideo()
291
292       const bitrate = await getVideoFileBitrate(tempFixturePath)
293       expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 60, VIDEO_TRANSCODING_FPS))
294     }
295
296     const videoAttributes = {
297       name: 'high bitrate video',
298       description: 'high bitrate video',
299       fixture: tempFixturePath
300     }
301
302     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
303
304     await waitJobs(servers)
305
306     for (const server of servers) {
307       const res = await getVideosList(server.url)
308
309       const video = res.body.data.find(v => v.name === videoAttributes.name)
310
311       for (const resolution of ['240', '360', '480', '720', '1080']) {
312         const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
313         const bitrate = await getVideoFileBitrate(path)
314         const fps = await getVideoFileFPS(path)
315         const resolution2 = await getVideoFileResolution(path)
316
317         expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
318         expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
319       }
320     }
321   })
322
323   it('Should accept and transcode additional extensions', async function () {
324     this.timeout(300000)
325
326     let tempFixturePath: string
327
328     {
329       tempFixturePath = await generateHighBitrateVideo()
330
331       const bitrate = await getVideoFileBitrate(tempFixturePath)
332       expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 60, VIDEO_TRANSCODING_FPS))
333     }
334
335     for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
336       const videoAttributes = {
337         name: fixture,
338         fixture
339       }
340
341       await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
342
343       await waitJobs(servers)
344
345       for (const server of servers) {
346         const res = await getVideosList(server.url)
347
348         const video = res.body.data.find(v => v.name === videoAttributes.name)
349         const res2 = await getVideo(server.url, video.id)
350         const videoDetails = res2.body
351
352         expect(videoDetails.files).to.have.lengthOf(4)
353
354         const magnetUri = videoDetails.files[ 0 ].magnetUri
355         expect(magnetUri).to.contain('.mp4')
356       }
357     }
358   })
359
360   it('Should correctly detect if quick transcode is possible', async function () {
361     this.timeout(10000)
362
363     expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.mp4'))).to.be.true
364     expect(await canDoQuickTranscode(buildAbsoluteFixturePath('video_short.webm'))).to.be.false
365   })
366
367   after(async function () {
368     await cleanupTests(servers)
369   })
370 })