File size: 1,412 Bytes
9fb8c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import json
import urllib.parse

from youtube_transcript_api import YouTubeTranscriptApi
import youtube_dl


def get_video_id(url):
    url_data = urllib.parse.urlparse(url)
    query = urllib.parse.parse_qs(url_data.query)
    id = query["v"][0]
    return id


def get_video_name(url):
    id = get_video_id(url)
    params = {"format": "json", "url": "https://www.youtube.com/watch?v=%s" % id}
    url = "https://www.youtube.com/oembed"
    query_string = urllib.parse.urlencode(params)
    url = url + "?" + query_string

    with urllib.request.urlopen(url) as response:
        response_text = response.read()
        data = json.loads(response_text.decode())
        return data['title']


def download_transcription(url, output_path):
    if os.path.exists(output_path):
        return

    id = get_video_id(url)

    # Download transcript with  'YouTubeTranscriptApi'
    str = YouTubeTranscriptApi.get_transcript(id, languages=['en'])
    json.dump(str, open(output_path, 'w'))


def download_mp3(url, output_path):
    if os.path.exists(output_path):
        return
    ydl_opts = {
        'outtmpl': output_path,
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])