aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/cli/create-transcoding-job.ts
blob: 53f187f90ad03210879d149f65c69e88c430ffae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import {
  cleanupTests,
  doubleFollow,
  createMultipleServers,
  PeerTubeServer,
  setAccessTokensToServers,
  waitJobs
} from '../../../shared/extra-utils'

const expect = chai.expect

describe('Test create transcoding jobs', function () {
  let servers: PeerTubeServer[] = []
  const videosUUID: string[] = []

  const config = {
    transcoding: {
      enabled: false,
      resolutions: {
        '240p': true,
        '360p': true,
        '480p': true,
        '720p': true,
        '1080p': true,
        '1440p': true,
        '2160p': true
      },
      hls: {
        enabled: false
      }
    }
  }

  before(async function () {
    this.timeout(60000)

    // Run server 2 to have transcoding enabled
    servers = await createMultipleServers(2)
    await setAccessTokensToServers(servers)

    await servers[0].config.updateCustomSubConfig({ newConfig: config })

    await doubleFollow(servers[0], servers[1])

    for (let i = 1; i <= 5; i++) {
      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' + i } })
      videosUUID.push(uuid)
    }

    await waitJobs(servers)
  })

  it('Should have two video files on each server', async function () {
    this.timeout(30000)

    for (const server of servers) {
      const { data } = await server.videos.list()
      expect(data).to.have.lengthOf(videosUUID.length)

      for (const video of data) {
        const videoDetail = await server.videos.get({ id: video.uuid })
        expect(videoDetail.files).to.have.lengthOf(1)
        expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
      }
    }
  })

  it('Should run a transcoding job on video 2', async function () {
    this.timeout(60000)

    await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
    await waitJobs(servers)

    for (const server of servers) {
      const { data } = await server.videos.list()

      let infoHashes: { [id: number]: string }

      for (const video of data) {
        const videoDetail = await server.videos.get({ id: video.uuid })

        if (video.uuid === videosUUID[1]) {
          expect(videoDetail.files).to.have.lengthOf(4)
          expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)

          if (!infoHashes) {
            infoHashes = {}

            for (const file of videoDetail.files) {
              infoHashes[file.resolution.id.toString()] = file.magnetUri
            }
          } else {
            for (const resolution of Object.keys(infoHashes)) {
              const file = videoDetail.files.find(f => f.resolution.id.toString() === resolution)
              expect(file.magnetUri).to.equal(infoHashes[resolution])
            }
          }
        } else {
          expect(videoDetail.files).to.have.lengthOf(1)
          expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
        }
      }
    }
  })

  it('Should run a transcoding job on video 1 with resolution', async function () {
    this.timeout(60000)

    await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)

    await waitJobs(servers)

    for (const server of servers) {
      const { data } = await server.videos.list()
      expect(data).to.have.lengthOf(videosUUID.length)

      const videoDetails = await server.videos.get({ id: videosUUID[0] })

      expect(videoDetails.files).to.have.lengthOf(2)
      expect(videoDetails.files[0].resolution.id).to.equal(720)
      expect(videoDetails.files[1].resolution.id).to.equal(480)

      expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
    }
  })

  it('Should generate an HLS resolution', async function () {
    this.timeout(120000)

    await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)

    await waitJobs(servers)

    for (const server of servers) {
      const videoDetails = await server.videos.get({ id: videosUUID[2] })

      expect(videoDetails.files).to.have.lengthOf(1)
      expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)

      const files = videoDetails.streamingPlaylists[0].files
      expect(files).to.have.lengthOf(1)
      expect(files[0].resolution.id).to.equal(480)
    }
  })

  it('Should not duplicate an HLS resolution', async function () {
    this.timeout(120000)

    await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)

    await waitJobs(servers)

    for (const server of servers) {
      const videoDetails = await server.videos.get({ id: videosUUID[2] })

      const files = videoDetails.streamingPlaylists[0].files
      expect(files).to.have.lengthOf(1)
      expect(files[0].resolution.id).to.equal(480)
    }
  })

  it('Should generate all HLS resolutions', async function () {
    this.timeout(120000)

    await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)

    await waitJobs(servers)

    for (const server of servers) {
      const videoDetails = await server.videos.get({ id: videosUUID[3] })

      expect(videoDetails.files).to.have.lengthOf(1)
      expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)

      const files = videoDetails.streamingPlaylists[0].files
      expect(files).to.have.lengthOf(4)
    }
  })

  it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
    this.timeout(120000)

    config.transcoding.hls.enabled = true
    await servers[0].config.updateCustomSubConfig({ newConfig: config })

    await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)

    await waitJobs(servers)

    for (const server of servers) {
      const videoDetails = await server.videos.get({ id: videosUUID[4] })

      expect(videoDetails.files).to.have.lengthOf(4)
      expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
      expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(4)
    }
  })

  after(async function () {
    await cleanupTests(servers)
  })
})