generateVideosMedia.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. // generate mock videos metadata for all the files in scripts/videos directory
  3. const path = require('path')
  4. const fs = require('fs')
  5. const { promisify } = require('util')
  6. const ffmpeg = require('fluent-ffmpeg')
  7. const faker = require('faker')
  8. const { saveToFile } = require('./utils')
  9. const readdir = promisify(fs.readdir)
  10. const ffprobe = promisify(ffmpeg.ffprobe)
  11. const HOSTED_ASSET_PREFIX = 'https://eu-central-1.linodeobjects.com/atlas-assets/videos/'
  12. const VIDEOS_DIR = 'videos'
  13. const OUTPUT_FILENAME = 'videosMedia.json'
  14. const videosPath = path.join(__dirname, VIDEOS_DIR)
  15. const CODEC_MAP = {
  16. h264: 'H264_mpeg4',
  17. vp8: 'VP8_WEBM',
  18. }
  19. const getVideoInfo = async (videoName) => {
  20. const videoPath = path.join(videosPath, videoName)
  21. const metadata = await ffprobe(videoPath)
  22. const stream = metadata.streams.find((s) => s.codec_type === 'video')
  23. return {
  24. id: faker.random.uuid(),
  25. codec: CODEC_MAP[stream.codec_name],
  26. pixelWidth: stream.width,
  27. pixelHeight: stream.height,
  28. duration: Math.floor(metadata.format.duration),
  29. size: metadata.format.size,
  30. location: {
  31. URL: HOSTED_ASSET_PREFIX + videoName,
  32. },
  33. }
  34. }
  35. const main = async () => {
  36. const videos = await readdir(videosPath)
  37. const promises = videos.map(getVideoInfo)
  38. const infos = await Promise.all(promises)
  39. await saveToFile(infos, OUTPUT_FILENAME)
  40. }
  41. main()