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