aboutsummaryrefslogtreecommitdiff
path: root/importer/providers/youtube_dl_base.py
blob: 3bb2fb80cd4e20cefad2ab16962ea4055ed5ee03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os

import youtube_dl

from importer.providers.providerbase import ProviderBase


class YoutubeDlProviderBase(ProviderBase):
    regex = ".*"
    output_template: str = 'source_%(id)s.%(ext)s'
    format: str = "best"
    merge_format_output: str = "mp4"

    _TEST = [{
        "url": "https://www.youtube.com/watch?v=Wjrrgrvq1ew",
        "paths": ["source_Wjrrgrvq1ew.mp4"]
    }]

    def download(self):
        ydl_opts = {
            'format': self.format,
            'merge_output_format': self.merge_format_output,
            'outtmpl': self.output_template
        }

        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(self.url, download=True)
            if info.get('_type', None) == 'playlist':
                for entry in info['entries']:
                    r = ydl.prepare_filename(entry)
                    self.paths.append(f'{os.path.splitext(r)[0]}.mp4')
            else:
                r = ydl.prepare_filename(info)
                self.paths.append(f'{os.path.splitext(r)[0]}.mp4')

            self.downloaded = True