]> jfr.im git - yt-dlp.git/blame - test/test_postprocessors.py
Add option `--replace-in-metadata`
[yt-dlp.git] / test / test_postprocessors.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
88cf6fb3
JMF
2
3from __future__ import unicode_literals
4
5# Allow direct execution
6import os
7import sys
8import unittest
9sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
752cda38 11from yt_dlp import YoutubeDL
12from yt_dlp.compat import compat_shlex_quote
337e0c62 13from yt_dlp.postprocessor import (
752cda38 14 ExecAfterDownloadPP,
337e0c62 15 FFmpegThumbnailsConvertorPP,
16 MetadataFromFieldPP,
e9f4ccd1 17 MetadataParserPP,
337e0c62 18)
5bfa4862 19
20
21class TestMetadataFromField(unittest.TestCase):
88cf6fb3 22
88cf6fb3 23 def test_format_to_regex(self):
e9f4ccd1 24 self.assertEqual(
25 MetadataParserPP.format_to_regex('%(title)s - %(artist)s'),
26 r'(?P<title>.+)\ \-\ (?P<artist>.+)')
27 self.assertEqual(MetadataParserPP.format_to_regex(r'(?P<x>.+)'), r'(?P<x>.+)')
28
29 def test_field_to_template(self):
30 self.assertEqual(MetadataParserPP.field_to_template('title'), '%(title)s')
31 self.assertEqual(MetadataParserPP.field_to_template('1'), '1')
32 self.assertEqual(MetadataParserPP.field_to_template('foo bar'), 'foo bar')
33 self.assertEqual(MetadataParserPP.field_to_template(' literal'), ' literal')
34
35 def test_metadatafromfield(self):
36 self.assertEqual(
37 MetadataFromFieldPP.to_action('%(title)s \\: %(artist)s:%(title)s : %(artist)s'),
38 (MetadataParserPP.Actions.INTERPRET, '%(title)s : %(artist)s', '%(title)s : %(artist)s'))
337e0c62 39
40
41class TestConvertThumbnail(unittest.TestCase):
42 def test_escaping(self):
43 pp = FFmpegThumbnailsConvertorPP()
44 if not pp.available:
45 print('Skipping: ffmpeg not found')
46 return
47
48 file = 'test/testdata/thumbnails/foo %d bar/foo_%d.{}'
49 tests = (('webp', 'png'), ('png', 'jpg'))
50
51 for inp, out in tests:
52 out_file = file.format(out)
53 if os.path.exists(out_file):
54 os.remove(out_file)
55 pp.convert_thumbnail(file.format(inp), out)
56 assert os.path.exists(out_file)
57
58 for _, out in tests:
59 os.remove(file.format(out))
752cda38 60
61
62class TestExecAfterDownload(unittest.TestCase):
63 def test_parse_cmd(self):
64 pp = ExecAfterDownloadPP(YoutubeDL(), '')
65 info = {'filepath': 'file name'}
66 quoted_filepath = compat_shlex_quote(info['filepath'])
67
68 self.assertEqual(pp.parse_cmd('echo', info), 'echo %s' % quoted_filepath)
69 self.assertEqual(pp.parse_cmd('echo.{}', info), 'echo.%s' % quoted_filepath)
70 self.assertEqual(pp.parse_cmd('echo "%(filepath)s"', info), 'echo "%s"' % info['filepath'])