test_postprocessors.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from yt_dlp import YoutubeDL
  8. from yt_dlp.utils import shell_quote
  9. from yt_dlp.postprocessor import (
  10. ExecPP,
  11. FFmpegThumbnailsConvertorPP,
  12. MetadataFromFieldPP,
  13. MetadataParserPP,
  14. ModifyChaptersPP,
  15. SponsorBlockPP,
  16. )
  17. class TestMetadataFromField(unittest.TestCase):
  18. def test_format_to_regex(self):
  19. self.assertEqual(
  20. MetadataParserPP.format_to_regex('%(title)s - %(artist)s'),
  21. r'(?P<title>.+)\ \-\ (?P<artist>.+)')
  22. self.assertEqual(MetadataParserPP.format_to_regex(r'(?P<x>.+)'), r'(?P<x>.+)')
  23. def test_field_to_template(self):
  24. self.assertEqual(MetadataParserPP.field_to_template('title'), '%(title)s')
  25. self.assertEqual(MetadataParserPP.field_to_template('1'), '1')
  26. self.assertEqual(MetadataParserPP.field_to_template('foo bar'), 'foo bar')
  27. self.assertEqual(MetadataParserPP.field_to_template(' literal'), ' literal')
  28. def test_metadatafromfield(self):
  29. self.assertEqual(
  30. MetadataFromFieldPP.to_action('%(title)s \\: %(artist)s:%(title)s : %(artist)s'),
  31. (MetadataParserPP.Actions.INTERPRET, '%(title)s : %(artist)s', '%(title)s : %(artist)s'))
  32. class TestConvertThumbnail(unittest.TestCase):
  33. def test_escaping(self):
  34. pp = FFmpegThumbnailsConvertorPP()
  35. if not pp.available:
  36. print('Skipping: ffmpeg not found')
  37. return
  38. file = 'test/testdata/thumbnails/foo %d bar/foo_%d.{}'
  39. tests = (('webp', 'png'), ('png', 'jpg'))
  40. for inp, out in tests:
  41. out_file = file.format(out)
  42. if os.path.exists(out_file):
  43. os.remove(out_file)
  44. pp.convert_thumbnail(file.format(inp), out)
  45. assert os.path.exists(out_file)
  46. for _, out in tests:
  47. os.remove(file.format(out))
  48. class TestExec(unittest.TestCase):
  49. def test_parse_cmd(self):
  50. pp = ExecPP(YoutubeDL(), '')
  51. info = {'filepath': 'file name'}
  52. cmd = 'echo {}'.format(shell_quote(info['filepath']))
  53. self.assertEqual(pp.parse_cmd('echo', info), cmd)
  54. self.assertEqual(pp.parse_cmd('echo {}', info), cmd)
  55. self.assertEqual(pp.parse_cmd('echo %(filepath)q', info), cmd)
  56. class TestModifyChaptersPP(unittest.TestCase):
  57. def setUp(self):
  58. self._pp = ModifyChaptersPP(YoutubeDL())
  59. @staticmethod
  60. def _sponsor_chapter(start, end, cat, remove=False, title=None):
  61. if title is None:
  62. title = SponsorBlockPP.CATEGORIES[cat]
  63. return {
  64. 'start_time': start,
  65. 'end_time': end,
  66. '_categories': [(cat, start, end, title)],
  67. **({'remove': True} if remove else {}),
  68. }
  69. @staticmethod
  70. def _chapter(start, end, title=None, remove=False):
  71. c = {'start_time': start, 'end_time': end}
  72. if title is not None:
  73. c['title'] = title
  74. if remove:
  75. c['remove'] = True
  76. return c
  77. def _chapters(self, ends, titles):
  78. self.assertEqual(len(ends), len(titles))
  79. start = 0
  80. chapters = []
  81. for e, t in zip(ends, titles):
  82. chapters.append(self._chapter(start, e, t))
  83. start = e
  84. return chapters
  85. def _remove_marked_arrange_sponsors_test_impl(
  86. self, chapters, expected_chapters, expected_removed):
  87. actual_chapters, actual_removed = (
  88. self._pp._remove_marked_arrange_sponsors(chapters))
  89. for c in actual_removed:
  90. c.pop('title', None)
  91. c.pop('_categories', None)
  92. actual_chapters = [{
  93. 'start_time': c['start_time'],
  94. 'end_time': c['end_time'],
  95. 'title': c['title'],
  96. } for c in actual_chapters]
  97. self.assertSequenceEqual(expected_chapters, actual_chapters)
  98. self.assertSequenceEqual(expected_removed, actual_removed)
  99. def test_remove_marked_arrange_sponsors_CanGetThroughUnaltered(self):
  100. chapters = self._chapters([10, 20, 30, 40], ['c1', 'c2', 'c3', 'c4'])
  101. self._remove_marked_arrange_sponsors_test_impl(chapters, chapters, [])
  102. def test_remove_marked_arrange_sponsors_ChapterWithSponsors(self):
  103. chapters = [
  104. *self._chapters([70], ['c']),
  105. self._sponsor_chapter(10, 20, 'sponsor'),
  106. self._sponsor_chapter(30, 40, 'preview'),
  107. self._sponsor_chapter(50, 60, 'filler')]
  108. expected = self._chapters(
  109. [10, 20, 30, 40, 50, 60, 70],
  110. ['c', '[SponsorBlock]: Sponsor', 'c', '[SponsorBlock]: Preview/Recap',
  111. 'c', '[SponsorBlock]: Filler Tangent', 'c'])
  112. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  113. def test_remove_marked_arrange_sponsors_SponsorBlockChapters(self):
  114. chapters = [
  115. *self._chapters([70], ['c']),
  116. self._sponsor_chapter(10, 20, 'chapter', title='sb c1'),
  117. self._sponsor_chapter(15, 16, 'chapter', title='sb c2'),
  118. self._sponsor_chapter(30, 40, 'preview'),
  119. self._sponsor_chapter(50, 60, 'filler')]
  120. expected = self._chapters(
  121. [10, 15, 16, 20, 30, 40, 50, 60, 70],
  122. ['c', '[SponsorBlock]: sb c1', '[SponsorBlock]: sb c1, sb c2', '[SponsorBlock]: sb c1',
  123. 'c', '[SponsorBlock]: Preview/Recap',
  124. 'c', '[SponsorBlock]: Filler Tangent', 'c'])
  125. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  126. def test_remove_marked_arrange_sponsors_UniqueNamesForOverlappingSponsors(self):
  127. chapters = [
  128. *self._chapters([120], ['c']),
  129. self._sponsor_chapter(10, 45, 'sponsor'),
  130. self._sponsor_chapter(20, 40, 'selfpromo'),
  131. self._sponsor_chapter(50, 70, 'sponsor'),
  132. self._sponsor_chapter(60, 85, 'selfpromo'),
  133. self._sponsor_chapter(90, 120, 'selfpromo'),
  134. self._sponsor_chapter(100, 110, 'sponsor')]
  135. expected = self._chapters(
  136. [10, 20, 40, 45, 50, 60, 70, 85, 90, 100, 110, 120],
  137. ['c', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Sponsor, Unpaid/Self Promotion',
  138. '[SponsorBlock]: Sponsor',
  139. 'c', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Sponsor, Unpaid/Self Promotion',
  140. '[SponsorBlock]: Unpaid/Self Promotion',
  141. 'c', '[SponsorBlock]: Unpaid/Self Promotion', '[SponsorBlock]: Unpaid/Self Promotion, Sponsor',
  142. '[SponsorBlock]: Unpaid/Self Promotion'])
  143. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  144. def test_remove_marked_arrange_sponsors_ChapterWithCuts(self):
  145. cuts = [self._chapter(10, 20, remove=True),
  146. self._sponsor_chapter(30, 40, 'sponsor', remove=True),
  147. self._chapter(50, 60, remove=True)]
  148. chapters = self._chapters([70], ['c']) + cuts
  149. self._remove_marked_arrange_sponsors_test_impl(
  150. chapters, self._chapters([40], ['c']), cuts)
  151. def test_remove_marked_arrange_sponsors_ChapterWithSponsorsAndCuts(self):
  152. chapters = [
  153. *self._chapters([70], ['c']),
  154. self._sponsor_chapter(10, 20, 'sponsor'),
  155. self._sponsor_chapter(30, 40, 'selfpromo', remove=True),
  156. self._sponsor_chapter(50, 60, 'interaction')]
  157. expected = self._chapters([10, 20, 40, 50, 60],
  158. ['c', '[SponsorBlock]: Sponsor', 'c',
  159. '[SponsorBlock]: Interaction Reminder', 'c'])
  160. self._remove_marked_arrange_sponsors_test_impl(
  161. chapters, expected, [self._chapter(30, 40, remove=True)])
  162. def test_remove_marked_arrange_sponsors_ChapterWithSponsorCutInTheMiddle(self):
  163. cuts = [self._sponsor_chapter(20, 30, 'selfpromo', remove=True),
  164. self._chapter(40, 50, remove=True)]
  165. chapters = [
  166. *self._chapters([70], ['c']),
  167. self._sponsor_chapter(10, 60, 'sponsor'),
  168. *cuts]
  169. expected = self._chapters(
  170. [10, 40, 50], ['c', '[SponsorBlock]: Sponsor', 'c'])
  171. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  172. def test_remove_marked_arrange_sponsors_ChapterWithCutHidingSponsor(self):
  173. cuts = [self._sponsor_chapter(20, 50, 'selfpromo', remove=True)]
  174. chapters = [
  175. *self._chapters([60], ['c']),
  176. self._sponsor_chapter(10, 20, 'intro'),
  177. self._sponsor_chapter(30, 40, 'sponsor'),
  178. self._sponsor_chapter(50, 60, 'outro'),
  179. *cuts]
  180. expected = self._chapters(
  181. [10, 20, 30], ['c', '[SponsorBlock]: Intermission/Intro Animation', '[SponsorBlock]: Endcards/Credits'])
  182. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  183. def test_remove_marked_arrange_sponsors_ChapterWithAdjacentSponsors(self):
  184. chapters = [
  185. *self._chapters([70], ['c']),
  186. self._sponsor_chapter(10, 20, 'sponsor'),
  187. self._sponsor_chapter(20, 30, 'selfpromo'),
  188. self._sponsor_chapter(30, 40, 'interaction')]
  189. expected = self._chapters(
  190. [10, 20, 30, 40, 70],
  191. ['c', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Unpaid/Self Promotion',
  192. '[SponsorBlock]: Interaction Reminder', 'c'])
  193. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  194. def test_remove_marked_arrange_sponsors_ChapterWithAdjacentCuts(self):
  195. chapters = [
  196. *self._chapters([70], ['c']),
  197. self._sponsor_chapter(10, 20, 'sponsor'),
  198. self._sponsor_chapter(20, 30, 'interaction', remove=True),
  199. self._chapter(30, 40, remove=True),
  200. self._sponsor_chapter(40, 50, 'selfpromo', remove=True),
  201. self._sponsor_chapter(50, 60, 'interaction')]
  202. expected = self._chapters([10, 20, 30, 40],
  203. ['c', '[SponsorBlock]: Sponsor',
  204. '[SponsorBlock]: Interaction Reminder', 'c'])
  205. self._remove_marked_arrange_sponsors_test_impl(
  206. chapters, expected, [self._chapter(20, 50, remove=True)])
  207. def test_remove_marked_arrange_sponsors_ChapterWithOverlappingSponsors(self):
  208. chapters = [
  209. *self._chapters([70], ['c']),
  210. self._sponsor_chapter(10, 30, 'sponsor'),
  211. self._sponsor_chapter(20, 50, 'selfpromo'),
  212. self._sponsor_chapter(40, 60, 'interaction')]
  213. expected = self._chapters(
  214. [10, 20, 30, 40, 50, 60, 70],
  215. ['c', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Sponsor, Unpaid/Self Promotion',
  216. '[SponsorBlock]: Unpaid/Self Promotion', '[SponsorBlock]: Unpaid/Self Promotion, Interaction Reminder',
  217. '[SponsorBlock]: Interaction Reminder', 'c'])
  218. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  219. def test_remove_marked_arrange_sponsors_ChapterWithOverlappingCuts(self):
  220. chapters = [
  221. *self._chapters([70], ['c']),
  222. self._sponsor_chapter(10, 30, 'sponsor', remove=True),
  223. self._sponsor_chapter(20, 50, 'selfpromo', remove=True),
  224. self._sponsor_chapter(40, 60, 'interaction', remove=True)]
  225. self._remove_marked_arrange_sponsors_test_impl(
  226. chapters, self._chapters([20], ['c']), [self._chapter(10, 60, remove=True)])
  227. def test_remove_marked_arrange_sponsors_ChapterWithRunsOfOverlappingSponsors(self):
  228. chapters = [
  229. *self._chapters([170], ['c']),
  230. self._sponsor_chapter(0, 30, 'intro'),
  231. self._sponsor_chapter(20, 50, 'sponsor'),
  232. self._sponsor_chapter(40, 60, 'selfpromo'),
  233. self._sponsor_chapter(70, 90, 'sponsor'),
  234. self._sponsor_chapter(80, 100, 'sponsor'),
  235. self._sponsor_chapter(90, 110, 'sponsor'),
  236. self._sponsor_chapter(120, 140, 'selfpromo'),
  237. self._sponsor_chapter(130, 160, 'interaction'),
  238. self._sponsor_chapter(150, 170, 'outro')]
  239. expected = self._chapters(
  240. [20, 30, 40, 50, 60, 70, 110, 120, 130, 140, 150, 160, 170],
  241. ['[SponsorBlock]: Intermission/Intro Animation', '[SponsorBlock]: Intermission/Intro Animation, Sponsor', '[SponsorBlock]: Sponsor',
  242. '[SponsorBlock]: Sponsor, Unpaid/Self Promotion', '[SponsorBlock]: Unpaid/Self Promotion', 'c',
  243. '[SponsorBlock]: Sponsor', 'c', '[SponsorBlock]: Unpaid/Self Promotion',
  244. '[SponsorBlock]: Unpaid/Self Promotion, Interaction Reminder',
  245. '[SponsorBlock]: Interaction Reminder',
  246. '[SponsorBlock]: Interaction Reminder, Endcards/Credits', '[SponsorBlock]: Endcards/Credits'])
  247. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  248. def test_remove_marked_arrange_sponsors_ChapterWithRunsOfOverlappingCuts(self):
  249. chapters = [
  250. *self._chapters([170], ['c']),
  251. self._chapter(0, 30, remove=True),
  252. self._sponsor_chapter(20, 50, 'sponsor', remove=True),
  253. self._chapter(40, 60, remove=True),
  254. self._sponsor_chapter(70, 90, 'sponsor', remove=True),
  255. self._chapter(80, 100, remove=True),
  256. self._chapter(90, 110, remove=True),
  257. self._sponsor_chapter(120, 140, 'sponsor', remove=True),
  258. self._sponsor_chapter(130, 160, 'selfpromo', remove=True),
  259. self._chapter(150, 170, remove=True)]
  260. expected_cuts = [self._chapter(0, 60, remove=True),
  261. self._chapter(70, 110, remove=True),
  262. self._chapter(120, 170, remove=True)]
  263. self._remove_marked_arrange_sponsors_test_impl(
  264. chapters, self._chapters([20], ['c']), expected_cuts)
  265. def test_remove_marked_arrange_sponsors_OverlappingSponsorsDifferentTitlesAfterCut(self):
  266. chapters = [
  267. *self._chapters([60], ['c']),
  268. self._sponsor_chapter(10, 60, 'sponsor'),
  269. self._sponsor_chapter(10, 40, 'intro'),
  270. self._sponsor_chapter(30, 50, 'interaction'),
  271. self._sponsor_chapter(30, 50, 'selfpromo', remove=True),
  272. self._sponsor_chapter(40, 50, 'interaction'),
  273. self._sponsor_chapter(50, 60, 'outro')]
  274. expected = self._chapters(
  275. [10, 30, 40], ['c', '[SponsorBlock]: Sponsor, Intermission/Intro Animation', '[SponsorBlock]: Sponsor, Endcards/Credits'])
  276. self._remove_marked_arrange_sponsors_test_impl(
  277. chapters, expected, [self._chapter(30, 50, remove=True)])
  278. def test_remove_marked_arrange_sponsors_SponsorsNoLongerOverlapAfterCut(self):
  279. chapters = [
  280. *self._chapters([70], ['c']),
  281. self._sponsor_chapter(10, 30, 'sponsor'),
  282. self._sponsor_chapter(20, 50, 'interaction'),
  283. self._sponsor_chapter(30, 50, 'selfpromo', remove=True),
  284. self._sponsor_chapter(40, 60, 'sponsor'),
  285. self._sponsor_chapter(50, 60, 'interaction')]
  286. expected = self._chapters(
  287. [10, 20, 40, 50], ['c', '[SponsorBlock]: Sponsor',
  288. '[SponsorBlock]: Sponsor, Interaction Reminder', 'c'])
  289. self._remove_marked_arrange_sponsors_test_impl(
  290. chapters, expected, [self._chapter(30, 50, remove=True)])
  291. def test_remove_marked_arrange_sponsors_SponsorsStillOverlapAfterCut(self):
  292. chapters = [
  293. *self._chapters([70], ['c']),
  294. self._sponsor_chapter(10, 60, 'sponsor'),
  295. self._sponsor_chapter(20, 60, 'interaction'),
  296. self._sponsor_chapter(30, 50, 'selfpromo', remove=True)]
  297. expected = self._chapters(
  298. [10, 20, 40, 50], ['c', '[SponsorBlock]: Sponsor',
  299. '[SponsorBlock]: Sponsor, Interaction Reminder', 'c'])
  300. self._remove_marked_arrange_sponsors_test_impl(
  301. chapters, expected, [self._chapter(30, 50, remove=True)])
  302. def test_remove_marked_arrange_sponsors_ChapterWithRunsOfOverlappingSponsorsAndCuts(self):
  303. chapters = [
  304. *self._chapters([200], ['c']),
  305. self._sponsor_chapter(10, 40, 'sponsor'),
  306. self._sponsor_chapter(10, 30, 'intro'),
  307. self._chapter(20, 30, remove=True),
  308. self._sponsor_chapter(30, 40, 'selfpromo'),
  309. self._sponsor_chapter(50, 70, 'sponsor'),
  310. self._sponsor_chapter(60, 80, 'interaction'),
  311. self._chapter(70, 80, remove=True),
  312. self._sponsor_chapter(70, 90, 'sponsor'),
  313. self._sponsor_chapter(80, 100, 'interaction'),
  314. self._sponsor_chapter(120, 170, 'selfpromo'),
  315. self._sponsor_chapter(130, 180, 'outro'),
  316. self._chapter(140, 150, remove=True),
  317. self._chapter(150, 160, remove=True)]
  318. expected = self._chapters(
  319. [10, 20, 30, 40, 50, 70, 80, 100, 110, 130, 140, 160],
  320. ['c', '[SponsorBlock]: Sponsor, Intermission/Intro Animation', '[SponsorBlock]: Sponsor, Unpaid/Self Promotion',
  321. 'c', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Sponsor, Interaction Reminder',
  322. '[SponsorBlock]: Interaction Reminder', 'c', '[SponsorBlock]: Unpaid/Self Promotion',
  323. '[SponsorBlock]: Unpaid/Self Promotion, Endcards/Credits', '[SponsorBlock]: Endcards/Credits', 'c'])
  324. expected_cuts = [self._chapter(20, 30, remove=True),
  325. self._chapter(70, 80, remove=True),
  326. self._chapter(140, 160, remove=True)]
  327. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, expected_cuts)
  328. def test_remove_marked_arrange_sponsors_SponsorOverlapsMultipleChapters(self):
  329. chapters = [
  330. *self._chapters([20, 40, 60, 80, 100], ['c1', 'c2', 'c3', 'c4', 'c5']),
  331. self._sponsor_chapter(10, 90, 'sponsor')]
  332. expected = self._chapters([10, 90, 100], ['c1', '[SponsorBlock]: Sponsor', 'c5'])
  333. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  334. def test_remove_marked_arrange_sponsors_CutOverlapsMultipleChapters(self):
  335. cuts = [self._chapter(10, 90, remove=True)]
  336. chapters = self._chapters([20, 40, 60, 80, 100], ['c1', 'c2', 'c3', 'c4', 'c5']) + cuts
  337. expected = self._chapters([10, 20], ['c1', 'c5'])
  338. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  339. def test_remove_marked_arrange_sponsors_SponsorsWithinSomeChaptersAndOverlappingOthers(self):
  340. chapters = [
  341. *self._chapters([10, 40, 60, 80], ['c1', 'c2', 'c3', 'c4']),
  342. self._sponsor_chapter(20, 30, 'sponsor'),
  343. self._sponsor_chapter(50, 70, 'selfpromo')]
  344. expected = self._chapters([10, 20, 30, 40, 50, 70, 80],
  345. ['c1', 'c2', '[SponsorBlock]: Sponsor', 'c2', 'c3',
  346. '[SponsorBlock]: Unpaid/Self Promotion', 'c4'])
  347. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  348. def test_remove_marked_arrange_sponsors_CutsWithinSomeChaptersAndOverlappingOthers(self):
  349. cuts = [self._chapter(20, 30, remove=True), self._chapter(50, 70, remove=True)]
  350. chapters = self._chapters([10, 40, 60, 80], ['c1', 'c2', 'c3', 'c4']) + cuts
  351. expected = self._chapters([10, 30, 40, 50], ['c1', 'c2', 'c3', 'c4'])
  352. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  353. def test_remove_marked_arrange_sponsors_ChaptersAfterLastSponsor(self):
  354. chapters = [
  355. *self._chapters([20, 40, 50, 60], ['c1', 'c2', 'c3', 'c4']),
  356. self._sponsor_chapter(10, 30, 'music_offtopic')]
  357. expected = self._chapters(
  358. [10, 30, 40, 50, 60],
  359. ['c1', '[SponsorBlock]: Non-Music Section', 'c2', 'c3', 'c4'])
  360. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  361. def test_remove_marked_arrange_sponsors_ChaptersAfterLastCut(self):
  362. cuts = [self._chapter(10, 30, remove=True)]
  363. chapters = self._chapters([20, 40, 50, 60], ['c1', 'c2', 'c3', 'c4']) + cuts
  364. expected = self._chapters([10, 20, 30, 40], ['c1', 'c2', 'c3', 'c4'])
  365. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  366. def test_remove_marked_arrange_sponsors_SponsorStartsAtChapterStart(self):
  367. chapters = [
  368. *self._chapters([10, 20, 40], ['c1', 'c2', 'c3']),
  369. self._sponsor_chapter(20, 30, 'sponsor')]
  370. expected = self._chapters([10, 20, 30, 40], ['c1', 'c2', '[SponsorBlock]: Sponsor', 'c3'])
  371. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  372. def test_remove_marked_arrange_sponsors_CutStartsAtChapterStart(self):
  373. cuts = [self._chapter(20, 30, remove=True)]
  374. chapters = self._chapters([10, 20, 40], ['c1', 'c2', 'c3']) + cuts
  375. expected = self._chapters([10, 20, 30], ['c1', 'c2', 'c3'])
  376. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  377. def test_remove_marked_arrange_sponsors_SponsorEndsAtChapterEnd(self):
  378. chapters = [
  379. *self._chapters([10, 30, 40], ['c1', 'c2', 'c3']),
  380. self._sponsor_chapter(20, 30, 'sponsor')]
  381. expected = self._chapters([10, 20, 30, 40], ['c1', 'c2', '[SponsorBlock]: Sponsor', 'c3'])
  382. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  383. def test_remove_marked_arrange_sponsors_CutEndsAtChapterEnd(self):
  384. cuts = [self._chapter(20, 30, remove=True)]
  385. chapters = self._chapters([10, 30, 40], ['c1', 'c2', 'c3']) + cuts
  386. expected = self._chapters([10, 20, 30], ['c1', 'c2', 'c3'])
  387. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  388. def test_remove_marked_arrange_sponsors_SponsorCoincidesWithChapters(self):
  389. chapters = [
  390. *self._chapters([10, 20, 30, 40], ['c1', 'c2', 'c3', 'c4']),
  391. self._sponsor_chapter(10, 30, 'sponsor')]
  392. expected = self._chapters([10, 30, 40], ['c1', '[SponsorBlock]: Sponsor', 'c4'])
  393. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  394. def test_remove_marked_arrange_sponsors_CutCoincidesWithChapters(self):
  395. cuts = [self._chapter(10, 30, remove=True)]
  396. chapters = self._chapters([10, 20, 30, 40], ['c1', 'c2', 'c3', 'c4']) + cuts
  397. expected = self._chapters([10, 20], ['c1', 'c4'])
  398. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  399. def test_remove_marked_arrange_sponsors_SponsorsAtVideoBoundaries(self):
  400. chapters = [
  401. *self._chapters([20, 40, 60], ['c1', 'c2', 'c3']),
  402. self._sponsor_chapter(0, 10, 'intro'), self._sponsor_chapter(50, 60, 'outro')]
  403. expected = self._chapters(
  404. [10, 20, 40, 50, 60], ['[SponsorBlock]: Intermission/Intro Animation', 'c1', 'c2', 'c3', '[SponsorBlock]: Endcards/Credits'])
  405. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  406. def test_remove_marked_arrange_sponsors_CutsAtVideoBoundaries(self):
  407. cuts = [self._chapter(0, 10, remove=True), self._chapter(50, 60, remove=True)]
  408. chapters = self._chapters([20, 40, 60], ['c1', 'c2', 'c3']) + cuts
  409. expected = self._chapters([10, 30, 40], ['c1', 'c2', 'c3'])
  410. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  411. def test_remove_marked_arrange_sponsors_SponsorsOverlapChaptersAtVideoBoundaries(self):
  412. chapters = [
  413. *self._chapters([10, 40, 50], ['c1', 'c2', 'c3']),
  414. self._sponsor_chapter(0, 20, 'intro'),
  415. self._sponsor_chapter(30, 50, 'outro')]
  416. expected = self._chapters(
  417. [20, 30, 50], ['[SponsorBlock]: Intermission/Intro Animation', 'c2', '[SponsorBlock]: Endcards/Credits'])
  418. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  419. def test_remove_marked_arrange_sponsors_CutsOverlapChaptersAtVideoBoundaries(self):
  420. cuts = [self._chapter(0, 20, remove=True), self._chapter(30, 50, remove=True)]
  421. chapters = self._chapters([10, 40, 50], ['c1', 'c2', 'c3']) + cuts
  422. expected = self._chapters([10], ['c2'])
  423. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, cuts)
  424. def test_remove_marked_arrange_sponsors_EverythingSponsored(self):
  425. chapters = [
  426. *self._chapters([10, 20, 30, 40], ['c1', 'c2', 'c3', 'c4']),
  427. self._sponsor_chapter(0, 20, 'intro'),
  428. self._sponsor_chapter(20, 40, 'outro')]
  429. expected = self._chapters([20, 40], ['[SponsorBlock]: Intermission/Intro Animation', '[SponsorBlock]: Endcards/Credits'])
  430. self._remove_marked_arrange_sponsors_test_impl(chapters, expected, [])
  431. def test_remove_marked_arrange_sponsors_EverythingCut(self):
  432. cuts = [self._chapter(0, 20, remove=True), self._chapter(20, 40, remove=True)]
  433. chapters = self._chapters([10, 20, 30, 40], ['c1', 'c2', 'c3', 'c4']) + cuts
  434. self._remove_marked_arrange_sponsors_test_impl(
  435. chapters, [], [self._chapter(0, 40, remove=True)])
  436. def test_remove_marked_arrange_sponsors_TinyChaptersInTheOriginalArePreserved(self):
  437. chapters = self._chapters([0.1, 0.2, 0.3, 0.4], ['c1', 'c2', 'c3', 'c4'])
  438. self._remove_marked_arrange_sponsors_test_impl(chapters, chapters, [])
  439. def test_remove_marked_arrange_sponsors_TinySponsorsAreIgnored(self):
  440. chapters = [self._sponsor_chapter(0, 0.1, 'intro'), self._chapter(0.1, 0.2, 'c1'),
  441. self._sponsor_chapter(0.2, 0.3, 'sponsor'), self._chapter(0.3, 0.4, 'c2'),
  442. self._sponsor_chapter(0.4, 0.5, 'outro')]
  443. self._remove_marked_arrange_sponsors_test_impl(
  444. chapters, self._chapters([0.3, 0.5], ['c1', 'c2']), [])
  445. def test_remove_marked_arrange_sponsors_TinyChaptersResultingFromCutsAreIgnored(self):
  446. cuts = [self._chapter(1.5, 2.5, remove=True)]
  447. chapters = self._chapters([2, 3, 3.5], ['c1', 'c2', 'c3']) + cuts
  448. self._remove_marked_arrange_sponsors_test_impl(
  449. chapters, self._chapters([2, 2.5], ['c1', 'c3']), cuts)
  450. def test_remove_marked_arrange_sponsors_SingleTinyChapterIsPreserved(self):
  451. cuts = [self._chapter(0.5, 2, remove=True)]
  452. chapters = self._chapters([2], ['c']) + cuts
  453. self._remove_marked_arrange_sponsors_test_impl(
  454. chapters, self._chapters([0.5], ['c']), cuts)
  455. def test_remove_marked_arrange_sponsors_TinyChapterAtTheStartPrependedToTheNext(self):
  456. cuts = [self._chapter(0.5, 2, remove=True)]
  457. chapters = self._chapters([2, 4], ['c1', 'c2']) + cuts
  458. self._remove_marked_arrange_sponsors_test_impl(
  459. chapters, self._chapters([2.5], ['c2']), cuts)
  460. def test_remove_marked_arrange_sponsors_TinyChaptersResultingFromSponsorOverlapAreIgnored(self):
  461. chapters = [
  462. *self._chapters([1, 3, 4], ['c1', 'c2', 'c3']),
  463. self._sponsor_chapter(1.5, 2.5, 'sponsor')]
  464. self._remove_marked_arrange_sponsors_test_impl(
  465. chapters, self._chapters([1.5, 2.5, 4], ['c1', '[SponsorBlock]: Sponsor', 'c3']), [])
  466. def test_remove_marked_arrange_sponsors_TinySponsorsOverlapsAreIgnored(self):
  467. chapters = [
  468. *self._chapters([2, 3, 5], ['c1', 'c2', 'c3']),
  469. self._sponsor_chapter(1, 3, 'sponsor'),
  470. self._sponsor_chapter(2.5, 4, 'selfpromo')]
  471. self._remove_marked_arrange_sponsors_test_impl(
  472. chapters, self._chapters([1, 3, 4, 5], [
  473. 'c1', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Unpaid/Self Promotion', 'c3']), [])
  474. def test_remove_marked_arrange_sponsors_TinySponsorsPrependedToTheNextSponsor(self):
  475. chapters = [
  476. *self._chapters([4], ['c']),
  477. self._sponsor_chapter(1.5, 2, 'sponsor'),
  478. self._sponsor_chapter(2, 4, 'selfpromo')]
  479. self._remove_marked_arrange_sponsors_test_impl(
  480. chapters, self._chapters([1.5, 4], ['c', '[SponsorBlock]: Unpaid/Self Promotion']), [])
  481. def test_remove_marked_arrange_sponsors_SmallestSponsorInTheOverlapGetsNamed(self):
  482. self._pp._sponsorblock_chapter_title = '[SponsorBlock]: %(name)s'
  483. chapters = [
  484. *self._chapters([10], ['c']),
  485. self._sponsor_chapter(2, 8, 'sponsor'),
  486. self._sponsor_chapter(4, 6, 'selfpromo')]
  487. self._remove_marked_arrange_sponsors_test_impl(
  488. chapters, self._chapters([2, 4, 6, 8, 10], [
  489. 'c', '[SponsorBlock]: Sponsor', '[SponsorBlock]: Unpaid/Self Promotion',
  490. '[SponsorBlock]: Sponsor', 'c',
  491. ]), [])
  492. def test_make_concat_opts_CommonCase(self):
  493. sponsor_chapters = [self._chapter(1, 2, 's1'), self._chapter(10, 20, 's2')]
  494. expected = '''ffconcat version 1.0
  495. file 'file:test'
  496. outpoint 1.000000
  497. file 'file:test'
  498. inpoint 2.000000
  499. outpoint 10.000000
  500. file 'file:test'
  501. inpoint 20.000000
  502. '''
  503. opts = self._pp._make_concat_opts(sponsor_chapters, 30)
  504. self.assertEqual(expected, ''.join(self._pp._concat_spec(['test'] * len(opts), opts)))
  505. def test_make_concat_opts_NoZeroDurationChunkAtVideoStart(self):
  506. sponsor_chapters = [self._chapter(0, 1, 's1'), self._chapter(10, 20, 's2')]
  507. expected = '''ffconcat version 1.0
  508. file 'file:test'
  509. inpoint 1.000000
  510. outpoint 10.000000
  511. file 'file:test'
  512. inpoint 20.000000
  513. '''
  514. opts = self._pp._make_concat_opts(sponsor_chapters, 30)
  515. self.assertEqual(expected, ''.join(self._pp._concat_spec(['test'] * len(opts), opts)))
  516. def test_make_concat_opts_NoZeroDurationChunkAtVideoEnd(self):
  517. sponsor_chapters = [self._chapter(1, 2, 's1'), self._chapter(10, 20, 's2')]
  518. expected = '''ffconcat version 1.0
  519. file 'file:test'
  520. outpoint 1.000000
  521. file 'file:test'
  522. inpoint 2.000000
  523. outpoint 10.000000
  524. '''
  525. opts = self._pp._make_concat_opts(sponsor_chapters, 20)
  526. self.assertEqual(expected, ''.join(self._pp._concat_spec(['test'] * len(opts), opts)))
  527. def test_quote_for_concat_RunsOfQuotes(self):
  528. self.assertEqual(
  529. r"'special '\'' '\'\''characters'\'\'\''galore'",
  530. self._pp._quote_for_ffmpeg("special ' ''characters'''galore"))
  531. def test_quote_for_concat_QuotesAtStart(self):
  532. self.assertEqual(
  533. r"\'\'\''special '\'' characters '\'' galore'",
  534. self._pp._quote_for_ffmpeg("'''special ' characters ' galore"))
  535. def test_quote_for_concat_QuotesAtEnd(self):
  536. self.assertEqual(
  537. r"'special '\'' characters '\'' galore'\'\'\'",
  538. self._pp._quote_for_ffmpeg("special ' characters ' galore'''"))