videoTrim.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. formatVideoSegments,
  4. formatVideoTime,
  5. normalizeVideoSegments,
  6. parseVideoTime,
  7. trimmedVideoLength,
  8. videoSegmentsOf
  9. } from './videoTrim'
  10. describe('formatVideoTime', () => {
  11. it('pads seconds and only shows hours when there are some', () => {
  12. expect(formatVideoTime(0)).toBe('0:00')
  13. expect(formatVideoTime(185)).toBe('3:05')
  14. expect(formatVideoTime(3723)).toBe('1:02:03')
  15. })
  16. it('keeps tenths of a second', () => {
  17. expect(formatVideoTime(65.5)).toBe('1:05.5')
  18. expect(formatVideoTime(59.96)).toBe('1:00')
  19. })
  20. })
  21. describe('parseVideoTime', () => {
  22. it('reads seconds, m:ss and h:mm:ss', () => {
  23. expect(parseVideoTime('90')).toBe(90)
  24. expect(parseVideoTime(' 3:00 ')).toBe(180)
  25. expect(parseVideoTime('0:01:30.5')).toBe(90.5)
  26. })
  27. it('rejects anything else', () => {
  28. expect(parseVideoTime('')).toBeNull()
  29. expect(parseVideoTime('1:')).toBeNull()
  30. expect(parseVideoTime('-5')).toBeNull()
  31. expect(parseVideoTime('1:2:3:4')).toBeNull()
  32. expect(parseVideoTime('abc')).toBeNull()
  33. })
  34. it('reads back what formatVideoTime writes', () => {
  35. for (const t of [0, 7.5, 185, 3723.4]) expect(parseVideoTime(formatVideoTime(t))).toBe(t)
  36. })
  37. })
  38. describe('trimmedVideoLength', () => {
  39. it('adds up the parts, an open end going to the end of the video', () => {
  40. expect(trimmedVideoLength(300, [{ start: 180, end: 240 }])).toBe(60)
  41. expect(trimmedVideoLength(300, [{ start: 180 }])).toBe(120)
  42. expect(trimmedVideoLength(300, [{ start: 120, end: 180 }, { start: 240, end: 300 }])).toBe(120)
  43. expect(trimmedVideoLength(300, [{ start: 100, end: 900 }])).toBe(200)
  44. })
  45. it('is the whole video without parts', () => {
  46. expect(trimmedVideoLength(300, [])).toBe(300)
  47. })
  48. })
  49. describe('videoSegmentsOf', () => {
  50. it('turns the old single start/end into one part', () => {
  51. expect(videoSegmentsOf({ videoStart: 180, videoEnd: 240 })).toEqual([{ start: 180, end: 240 }])
  52. expect(videoSegmentsOf({ videoEnd: 60 })).toEqual([{ start: 0, end: 60 }])
  53. expect(videoSegmentsOf({})).toEqual([])
  54. })
  55. it('prefers the parts list', () => {
  56. expect(videoSegmentsOf({ videoSegments: [], videoStart: 180 })).toEqual([])
  57. })
  58. })
  59. describe('normalizeVideoSegments', () => {
  60. it('sorts, drops empty parts and merges overlapping ones', () => {
  61. expect(
  62. normalizeVideoSegments([
  63. { start: 240, end: 300 },
  64. { start: 120, end: 180 },
  65. { start: 150, end: 200 },
  66. { start: 50, end: 50 }
  67. ])
  68. ).toEqual([
  69. { start: 120, end: 200 },
  70. { start: 240, end: 300 }
  71. ])
  72. })
  73. it('keeps touching parts apart', () => {
  74. expect(normalizeVideoSegments([{ start: 0, end: 30 }, { start: 30, end: 60 }])).toEqual([
  75. { start: 0, end: 30 },
  76. { start: 30, end: 60 }
  77. ])
  78. })
  79. it('lets an open end swallow the parts after it', () => {
  80. expect(normalizeVideoSegments([{ start: 10 }, { start: 60, end: 70 }])).toEqual([{ start: 10 }])
  81. })
  82. it('opens an end at the end of the video and drops parts past it', () => {
  83. expect(normalizeVideoSegments([{ start: 120, end: 180 }, { start: 240, end: 300 }, { start: 400 }], 300)).toEqual([
  84. { start: 120, end: 180 },
  85. { start: 240 }
  86. ])
  87. })
  88. it('is no parts for the whole video', () => {
  89. expect(normalizeVideoSegments([{ start: 0, end: 300 }], 300)).toEqual([])
  90. })
  91. })
  92. describe('formatVideoSegments', () => {
  93. it('writes what --video-segments reads', () => {
  94. expect(formatVideoSegments([{ start: 120, end: 180 }, { start: 240.25 }])).toBe('120-180,240.25-')
  95. })
  96. })