]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-transcoder.ts
adding tests for audio conversions
[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
RK
5import { omit } from 'lodash'
6import * as ffmpeg from 'fluent-ffmpeg'
2186386c 7import { VideoDetails, VideoState } from '../../../../shared/models/videos'
7160878c 8import { getVideoFileFPS, audio } from '../../../helpers/ffmpeg-utils'
0e1dc3e7 9import {
7160878c 10 buildAbsoluteFixturePath,
2186386c
C
11 doubleFollow,
12 flushAndRunMultipleServers,
2186386c
C
13 getMyVideos,
14 getVideo,
15 getVideosList,
16 killallServers,
17 root,
18 ServerInfo,
19 setAccessTokensToServers,
20 uploadVideo,
2186386c 21 webtorrentAdd
a7ba16b6 22} from '../../utils'
73c69591 23import { join } from 'path'
3cd0734f 24import { waitJobs } from '../../utils/server/jobs'
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)
38 })
39
40 it('Should not transcode video on server 1', async function () {
41 this.timeout(60000)
42
43 const videoAttributes = {
975e6e0e
C
44 name: 'my super name for server 1',
45 description: 'my super description for server 1',
0e1dc3e7
C
46 fixture: 'video_short.webm'
47 }
48 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
49
3cd0734f 50 await waitJobs(servers)
0e1dc3e7
C
51
52 const res = await getVideosList(servers[0].url)
53 const video = res.body.data[0]
40298b02 54
5f04dd2f
C
55 const res2 = await getVideo(servers[0].url, video.id)
56 const videoDetails = res2.body
57 expect(videoDetails.files).to.have.lengthOf(1)
58
59 const magnetUri = videoDetails.files[0].magnetUri
0e1dc3e7
C
60 expect(magnetUri).to.match(/\.webm/)
61
62 const torrent = await webtorrentAdd(magnetUri)
63 expect(torrent.files).to.be.an('array')
64 expect(torrent.files.length).to.equal(1)
65 expect(torrent.files[0].path).match(/\.webm$/)
66 })
67
68 it('Should transcode video on server 2', async function () {
69 this.timeout(60000)
70
71 const videoAttributes = {
975e6e0e
C
72 name: 'my super name for server 2',
73 description: 'my super description for server 2',
0e1dc3e7
C
74 fixture: 'video_short.webm'
75 }
76 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
77
3cd0734f 78 await waitJobs(servers)
0e1dc3e7
C
79
80 const res = await getVideosList(servers[1].url)
81
82 const video = res.body.data[0]
5f04dd2f
C
83 const res2 = await getVideo(servers[1].url, video.id)
84 const videoDetails = res2.body
85
86 expect(videoDetails.files).to.have.lengthOf(4)
40298b02 87
5f04dd2f 88 const magnetUri = videoDetails.files[0].magnetUri
0e1dc3e7
C
89 expect(magnetUri).to.match(/\.mp4/)
90
91 const torrent = await webtorrentAdd(magnetUri)
92 expect(torrent.files).to.be.an('array')
93 expect(torrent.files.length).to.equal(1)
94 expect(torrent.files[0].path).match(/\.mp4$/)
95 })
96
7160878c
RK
97 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
98 this.timeout(60000)
99
100 const videoAttributes = {
101 name: 'mp3_256k',
102 fixture: 'video_short_mp3_256k.mp4'
103 }
104 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
105
106 await waitJobs(servers)
107
108 const res = await getVideosList(servers[1].url)
109
110 const video = res.body.data.find(v => v.name === videoAttributes.name)
111 const res2 = await getVideo(servers[1].url, video.id)
112 const videoDetails: VideoDetails = res2.body
113
114 expect(videoDetails.files).to.have.lengthOf(4)
115
116 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
117 const probe = await audio.get(ffmpeg, path)
118
119 if (probe.audioStream) {
120 expect(probe.audioStream['codec_name']).to.be.equal('aac')
121 expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
122 } else {
123 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
124 }
125 })
126
127 it('Should transcode video with no audio and have no audio itself', async function () {
128 this.timeout(60000)
129
130 const videoAttributes = {
131 name: 'no_audio',
132 fixture: 'video_short_no_audio.mp4'
133 }
134 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
135
136 await waitJobs(servers)
137
138 const res = await getVideosList(servers[1].url)
139
140 const video = res.body.data.find(v => v.name === videoAttributes.name)
141 const res2 = await getVideo(servers[1].url, video.id)
142 const videoDetails: VideoDetails = res2.body
143
144 expect(videoDetails.files).to.have.lengthOf(4)
145 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
146 const probe = await audio.get(ffmpeg, path)
147 expect(probe).to.not.have.property('audioStream')
148 })
149
150 it('Should leave the audio untouched, but properly transcode the video', async function () {
151 this.timeout(60000)
152
153 const videoAttributes = {
154 name: 'untouched_audio',
155 fixture: 'video_short.mp4'
156 }
157 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
158
159 await waitJobs(servers)
160
161 const res = await getVideosList(servers[1].url)
162
163 const video = res.body.data.find(v => v.name === videoAttributes.name)
164 const res2 = await getVideo(servers[1].url, video.id)
165 const videoDetails: VideoDetails = res2.body
166
167 expect(videoDetails.files).to.have.lengthOf(4)
168 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
169 const fixtureVideoProbe = await audio.get(ffmpeg, fixturePath)
170 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
171 const videoProbe = await audio.get(ffmpeg, path)
172 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
173 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
174 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
175 } else {
176 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
177 }
178 })
179
3a6f351b 180 it('Should transcode a 60 FPS video', async function () {
73c69591
C
181 this.timeout(60000)
182
183 const videoAttributes = {
184 name: 'my super 30fps name for server 2',
185 description: 'my super 30fps description for server 2',
3a6f351b 186 fixture: '60fps_720p_small.mp4'
73c69591
C
187 }
188 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
189
3cd0734f 190 await waitJobs(servers)
73c69591
C
191
192 const res = await getVideosList(servers[1].url)
193
7160878c 194 const video = res.body.data.find(v => v.name === videoAttributes.name)
73c69591
C
195 const res2 = await getVideo(servers[1].url, video.id)
196 const videoDetails: VideoDetails = res2.body
197
3a6f351b
C
198 expect(videoDetails.files).to.have.lengthOf(4)
199 expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
200 expect(videoDetails.files[1].fps).to.be.below(31)
201 expect(videoDetails.files[2].fps).to.be.below(31)
202 expect(videoDetails.files[3].fps).to.be.below(31)
73c69591 203
3a6f351b 204 for (const resolution of [ '240', '360', '480' ]) {
73c69591
C
205 const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
206 const fps = await getVideoFileFPS(path)
207
208 expect(fps).to.be.below(31)
209 }
3a6f351b
C
210
211 const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
212 const fps = await getVideoFileFPS(path)
213
214 expect(fps).to.be.above(58).and.below(62)
73c69591
C
215 })
216
2186386c
C
217 it('Should wait transcoding before publishing the video', async function () {
218 this.timeout(80000)
219
220 await doubleFollow(servers[0], servers[1])
221
3cd0734f 222 await waitJobs(servers)
2186386c
C
223
224 {
225 // Upload the video, but wait transcoding
226 const videoAttributes = {
227 name: 'waiting video',
228 fixture: 'video_short1.webm',
229 waitTranscoding: true
230 }
231 const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
232 const videoId = resVideo.body.video.uuid
233
234 // Should be in transcode state
235 const { body } = await getVideo(servers[ 1 ].url, videoId)
236 expect(body.name).to.equal('waiting video')
237 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
238 expect(body.state.label).to.equal('To transcode')
239 expect(body.waitTranscoding).to.be.true
240
241 // Should have my video
242 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
7160878c 243 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
244 expect(videoToFindInMine).not.to.be.undefined
245 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
246 expect(videoToFindInMine.state.label).to.equal('To transcode')
247 expect(videoToFindInMine.waitTranscoding).to.be.true
248
249 // Should not list this video
250 const resVideos = await getVideosList(servers[1].url)
7160878c 251 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
252 expect(videoToFindInList).to.be.undefined
253
254 // Server 1 should not have the video yet
255 await getVideo(servers[0].url, videoId, 404)
256 }
257
3cd0734f 258 await waitJobs(servers)
2186386c
C
259
260 for (const server of servers) {
261 const res = await getVideosList(server.url)
262 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
263 expect(videoToFind).not.to.be.undefined
264
265 const res2 = await getVideo(server.url, videoToFind.id)
266 const videoDetails: VideoDetails = res2.body
267
268 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
269 expect(videoDetails.state.label).to.equal('Published')
270 expect(videoDetails.waitTranscoding).to.be.true
271 }
272 })
273
0e1dc3e7
C
274 after(async function () {
275 killallServers(servers)
0e1dc3e7
C
276 })
277})