]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-transcoder.ts
fixed formatting, added test case
[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'
7ed2c1a4 7import { audio, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, canDoQuickTranscode } from '../../../helpers/ffmpeg-utils'
0e1dc3e7 8import {
7c3b7976 9 buildAbsoluteFixturePath, cleanupTests,
2186386c
C
10 doubleFollow,
11 flushAndRunMultipleServers,
d175a6f7 12 generateHighBitrateVideo,
2186386c
C
13 getMyVideos,
14 getVideo,
15 getVideosList,
7ed2c1a4 16 waitJobs,
2186386c
C
17 root,
18 ServerInfo,
19 setAccessTokensToServers,
20 uploadVideo,
d175a6f7 21 webtorrentAdd
94565d52 22} from '../../../../shared/extra-utils'
7ed2c1a4 23import { join } from 'path'
edb4ffc7 24import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
a7ba16b6
C
25
26const expect = chai.expect
0e1dc3e7
C
27
28describe('Test video transcoding', function () {
29 let servers: ServerInfo[] = []
30
31 before(async function () {
e212f887 32 this.timeout(30000)
0e1dc3e7
C
33
34 // Run servers
35 servers = await flushAndRunMultipleServers(2)
36
37 await setAccessTokensToServers(servers)
b2977eec
C
38
39 await doubleFollow(servers[0], servers[1])
0e1dc3e7
C
40 })
41
42 it('Should not transcode video on server 1', async function () {
43 this.timeout(60000)
44
45 const videoAttributes = {
975e6e0e
C
46 name: 'my super name for server 1',
47 description: 'my super description for server 1',
0e1dc3e7
C
48 fixture: 'video_short.webm'
49 }
50 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
51
3cd0734f 52 await waitJobs(servers)
0e1dc3e7 53
b2977eec
C
54 for (const server of servers) {
55 const res = await getVideosList(server.url)
56 const video = res.body.data[ 0 ]
40298b02 57
b2977eec
C
58 const res2 = await getVideo(server.url, video.id)
59 const videoDetails = res2.body
60 expect(videoDetails.files).to.have.lengthOf(1)
5f04dd2f 61
b2977eec
C
62 const magnetUri = videoDetails.files[ 0 ].magnetUri
63 expect(magnetUri).to.match(/\.webm/)
0e1dc3e7 64
b2977eec
C
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 }
0e1dc3e7
C
70 })
71
72 it('Should transcode video on server 2', async function () {
73 this.timeout(60000)
74
75 const videoAttributes = {
975e6e0e
C
76 name: 'my super name for server 2',
77 description: 'my super description for server 2',
0e1dc3e7
C
78 fixture: 'video_short.webm'
79 }
80 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
81
3cd0734f 82 await waitJobs(servers)
0e1dc3e7 83
b2977eec
C
84 for (const server of servers) {
85 const res = await getVideosList(server.url)
0e1dc3e7 86
b2977eec
C
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
5f04dd2f 90
b2977eec 91 expect(videoDetails.files).to.have.lengthOf(4)
40298b02 92
b2977eec
C
93 const magnetUri = videoDetails.files[ 0 ].magnetUri
94 expect(magnetUri).to.match(/\.mp4/)
0e1dc3e7 95
b2977eec
C
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 }
0e1dc3e7
C
101 })
102
7160878c
RK
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
b2977eec
C
114 for (const server of servers) {
115 const res = await getVideosList(server.url)
7160878c 116
b2977eec
C
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
7160878c 120
b2977eec 121 expect(videoDetails.files).to.have.lengthOf(4)
7160878c 122
b2977eec 123 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
cdf4cb9e 124 const probe = await audio.get(path)
7160878c 125
b2977eec
C
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 }
7160878c
RK
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
b2977eec
C
146 for (const server of servers) {
147 const res = await getVideosList(server.url)
7160878c 148
b2977eec
C
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
7160878c 152
b2977eec
C
153 expect(videoDetails.files).to.have.lengthOf(4)
154 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
cdf4cb9e 155 const probe = await audio.get(path)
b2977eec
C
156 expect(probe).to.not.have.property('audioStream')
157 }
7160878c
RK
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
b2977eec
C
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)
cdf4cb9e 180 const fixtureVideoProbe = await audio.get(fixturePath)
b2977eec 181 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
cdf4cb9e 182 const videoProbe = await audio.get(path)
b2977eec
C
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 }
7160878c
RK
189 }
190 })
191
3a6f351b 192 it('Should transcode a 60 FPS video', async function () {
73c69591
C
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',
3a6f351b 198 fixture: '60fps_720p_small.mp4'
73c69591
C
199 }
200 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
201
3cd0734f 202 await waitJobs(servers)
73c69591 203
b2977eec
C
204 for (const server of servers) {
205 const res = await getVideosList(server.url)
73c69591 206
b2977eec
C
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
73c69591 210
b2977eec
C
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)
73c69591 216
b2977eec
C
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)
73c69591 220
b2977eec
C
221 expect(fps).to.be.below(31)
222 }
3a6f351b 223
b2977eec
C
224 const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
225 const fps = await getVideoFileFPS(path)
3a6f351b 226
b2977eec
C
227 expect(fps).to.be.above(58).and.below(62)
228 }
73c69591
C
229 })
230
edb4ffc7 231 it('Should wait for transcoding before publishing the video', async function () {
2186386c
C
232 this.timeout(80000)
233
2186386c
C
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)
7160878c 253 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
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)
7160878c 261 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
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
3cd0734f 268 await waitJobs(servers)
2186386c
C
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
edb4ffc7
FA
284 it('Should respect maximum bitrate values', async function () {
285 this.timeout(160000)
286
74cd011b
C
287 let tempFixturePath: string
288
edb4ffc7 289 {
74cd011b 290 tempFixturePath = await generateHighBitrateVideo()
edb4ffc7
FA
291
292 const bitrate = await getVideoFileBitrate(tempFixturePath)
293 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 60, VIDEO_TRANSCODING_FPS))
c1c86c15 294 }
edb4ffc7 295
c1c86c15
C
296 const videoAttributes = {
297 name: 'high bitrate video',
298 description: 'high bitrate video',
299 fixture: tempFixturePath
300 }
edb4ffc7 301
c1c86c15 302 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
edb4ffc7 303
c1c86c15 304 await waitJobs(servers)
edb4ffc7 305
c1c86c15
C
306 for (const server of servers) {
307 const res = await getVideosList(server.url)
edb4ffc7 308
c1c86c15 309 const video = res.body.data.find(v => v.name === videoAttributes.name)
edb4ffc7 310
c1c86c15
C
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)
edb4ffc7 316
c1c86c15
C
317 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
318 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
edb4ffc7
FA
319 }
320 }
321 })
322
14e2014a
C
323 it('Should accept and transcode additional extensions', async function () {
324 this.timeout(300000)
325
7ed2c1a4
FA
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
14e2014a
C
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
7ed2c1a4
FA
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
7c3b7976
C
367 after(async function () {
368 await cleanupTests(servers)
0e1dc3e7
C
369 })
370})