optimize-old-videos.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import * as chai from 'chai'
  4. import { getMaxBitrate, Video, VideoDetails, VideoResolution } from '../../../shared/models/videos'
  5. import {
  6. cleanupTests,
  7. doubleFollow,
  8. execCLI,
  9. flushAndRunMultipleServers,
  10. generateHighBitrateVideo,
  11. getEnvCli,
  12. getVideo,
  13. getVideosList,
  14. root,
  15. ServerInfo,
  16. setAccessTokensToServers,
  17. uploadVideo,
  18. viewVideo,
  19. wait
  20. } from '../../../shared/extra-utils'
  21. import { waitJobs } from '../../../shared/extra-utils/server/jobs'
  22. import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../../helpers/ffmpeg-utils'
  23. import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants'
  24. import { join } from 'path'
  25. const expect = chai.expect
  26. describe('Test optimize old videos', function () {
  27. let servers: ServerInfo[] = []
  28. let video1UUID: string
  29. let video2UUID: string
  30. before(async function () {
  31. this.timeout(200000)
  32. // Run server 2 to have transcoding enabled
  33. servers = await flushAndRunMultipleServers(2)
  34. await setAccessTokensToServers(servers)
  35. await doubleFollow(servers[0], servers[1])
  36. let tempFixturePath: string
  37. {
  38. tempFixturePath = await generateHighBitrateVideo()
  39. const bitrate = await getVideoFileBitrate(tempFixturePath)
  40. expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 60, VIDEO_TRANSCODING_FPS))
  41. }
  42. // Upload two videos for our needs
  43. const res1 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video1', fixture: tempFixturePath })
  44. video1UUID = res1.body.video.uuid
  45. const res2 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video2', fixture: tempFixturePath })
  46. video2UUID = res2.body.video.uuid
  47. await waitJobs(servers)
  48. })
  49. it('Should have two video files on each server', async function () {
  50. this.timeout(30000)
  51. for (const server of servers) {
  52. const res = await getVideosList(server.url)
  53. const videos = res.body.data
  54. expect(videos).to.have.lengthOf(2)
  55. for (const video of videos) {
  56. const res2 = await getVideo(server.url, video.uuid)
  57. const videoDetail: VideoDetails = res2.body
  58. expect(videoDetail.files).to.have.lengthOf(1)
  59. }
  60. }
  61. })
  62. it('Should run optimize script', async function () {
  63. this.timeout(120000)
  64. const env = getEnvCli(servers[0])
  65. await execCLI(`${env} npm run optimize-old-videos`)
  66. await waitJobs(servers)
  67. for (const server of servers) {
  68. const res = await getVideosList(server.url)
  69. const videos: Video[] = res.body.data
  70. expect(videos).to.have.lengthOf(2)
  71. for (const video of videos) {
  72. await viewVideo(server.url, video.uuid)
  73. // Refresh video
  74. await waitJobs(servers)
  75. await wait(5000)
  76. await waitJobs(servers)
  77. const res2 = await getVideo(server.url, video.uuid)
  78. const videosDetails: VideoDetails = res2.body
  79. expect(videosDetails.files).to.have.lengthOf(1)
  80. const file = videosDetails.files[0]
  81. expect(file.size).to.be.below(5000000)
  82. const path = join(root(), 'test' + servers[0].internalServerNumber, 'videos', video.uuid + '-' + file.resolution.id + '.mp4')
  83. const bitrate = await getVideoFileBitrate(path)
  84. const fps = await getVideoFileFPS(path)
  85. const resolution = await getVideoFileResolution(path)
  86. expect(resolution.videoFileResolution).to.equal(file.resolution.id)
  87. expect(bitrate).to.be.below(getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
  88. }
  89. }
  90. })
  91. after(async function () {
  92. await cleanupTests(servers)
  93. })
  94. })