Add 30 fps limit in transcoding
[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 { VideoDetails } from '../../../../shared/models/videos'
6 import { getVideoFileFPS } from '../../../helpers/ffmpeg-utils'
7 import {
8   flushAndRunMultipleServers, flushTests, getVideo, getVideosList, killallServers, root, ServerInfo, setAccessTokensToServers, uploadVideo,
9   wait, webtorrentAdd
10 } from '../../utils'
11 import { join } from 'path'
12
13 const expect = chai.expect
14
15 describe('Test video transcoding', function () {
16   let servers: ServerInfo[] = []
17
18   before(async function () {
19     this.timeout(30000)
20
21     // Run servers
22     servers = await flushAndRunMultipleServers(2)
23
24     await setAccessTokensToServers(servers)
25   })
26
27   it('Should not transcode video on server 1', async function () {
28     this.timeout(60000)
29
30     const videoAttributes = {
31       name: 'my super name for server 1',
32       description: 'my super description for server 1',
33       fixture: 'video_short.webm'
34     }
35     await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
36
37     await wait(10000)
38
39     const res = await getVideosList(servers[0].url)
40     const video = res.body.data[0]
41
42     const res2 = await getVideo(servers[0].url, video.id)
43     const videoDetails = res2.body
44     expect(videoDetails.files).to.have.lengthOf(1)
45
46     const magnetUri = videoDetails.files[0].magnetUri
47     expect(magnetUri).to.match(/\.webm/)
48
49     const torrent = await webtorrentAdd(magnetUri)
50     expect(torrent.files).to.be.an('array')
51     expect(torrent.files.length).to.equal(1)
52     expect(torrent.files[0].path).match(/\.webm$/)
53   })
54
55   it('Should transcode video on server 2', async function () {
56     this.timeout(60000)
57
58     const videoAttributes = {
59       name: 'my super name for server 2',
60       description: 'my super description for server 2',
61       fixture: 'video_short.webm'
62     }
63     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
64
65     await wait(20000)
66
67     const res = await getVideosList(servers[1].url)
68
69     const video = res.body.data[0]
70     const res2 = await getVideo(servers[1].url, video.id)
71     const videoDetails = res2.body
72
73     expect(videoDetails.files).to.have.lengthOf(4)
74
75     const magnetUri = videoDetails.files[0].magnetUri
76     expect(magnetUri).to.match(/\.mp4/)
77
78     const torrent = await webtorrentAdd(magnetUri)
79     expect(torrent.files).to.be.an('array')
80     expect(torrent.files.length).to.equal(1)
81     expect(torrent.files[0].path).match(/\.mp4$/)
82   })
83
84   it('Should transcode to 30 FPS', async function () {
85     this.timeout(60000)
86
87     const videoAttributes = {
88       name: 'my super 30fps name for server 2',
89       description: 'my super 30fps description for server 2',
90       fixture: 'video_60fps_short.mp4'
91     }
92     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
93
94     await wait(20000)
95
96     const res = await getVideosList(servers[1].url)
97
98     const video = res.body.data[0]
99     const res2 = await getVideo(servers[1].url, video.id)
100     const videoDetails: VideoDetails = res2.body
101
102     expect(videoDetails.files).to.have.lengthOf(1)
103
104     for (const resolution of [ '240' ]) {
105       const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
106       const fps = await getVideoFileFPS(path)
107
108       expect(fps).to.be.below(31)
109     }
110   })
111
112   after(async function () {
113     killallServers(servers)
114
115     // Keep the logs if the test failed
116     if (this['ok']) {
117       await flushTests()
118     }
119   })
120 })