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