Công nghệ Mẹo vặt Sói's Tutorials Ý tưởng

[python] Tool để download, transcript Youtube by soiqualang

Table of Content

[python] Tool để download, transcript Youtube by soiqualang

Tool này mình viết cho Youtube dùng để:

  • Download a video
  • Download all video in a playlist
  • Download mp3
  • Convert video to transcript
    ...

Còn tiếp tục update

Open In Colab

Python tool for Youtube

  • Download video form Youtube
  • Download mp3 from Youtube
  • Get list video from Youtube playlist
In [0]:
#youtube 2 mp3
!pip install youtube_dl

import requests
from __future__ import unicode_literals
import youtube_dl

####

def youtube2video(download_path,youtube_url):
    Location = '%s soiqualang_%(extractor)s-%(id)s-%(title)s.%(ext)s'.replace("%s ", download_path)
    ytdl_format_options = {
        'outtmpl': Location
    }
    with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
         ydl.download([youtube_url])
            
def youtube2mp3(download_path,youtube_url):
    Location = '%s soiqualang_%(extractor)s-%(id)s-%(title)s.%(ext)s'.replace("%s ", download_path)
    ydl_opts = {
        'outtmpl': Location,
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([youtube_url])
        
def get_listvideo(url):
    #url="https://www.youtube.com/channel/UCIKTHNRRnVP5d8FpDI5YhXQ/videos"
    page = requests.get(url).content
    data = str(page).split(' ')
    item = 'href="/watch?'
    vids = [line.replace('href="', 'youtube.com') for line in data if item in line] # list of all videos listed twice
    #print(vids[0]) # index the latest video
    return vids
In [0]:
func_selected = "Download video" #@param ["Download video", "Download mp3", "Get all video in list", "Video to text"]
youtube_url = 'http://www.youtube.com/watch?v=BaW_jenozKc'  #@param {type: "string"}
download_path = 'download'  #@param {type: "string"}

finalchar=download_path[len(download_path)-1]
if(finalchar!='/'):
    download_path+='/'
    

######

if(func_selected=='Download video'):
    youtube2video(download_path,youtube_url)
elif(func_selected=='Download mp3'):
    youtube2mp3(download_path,youtube_url)
elif(func_selected=='Get all video in list'):
    lvideo=get_listvideo(youtube_url)
    print(lvideo)
elif(func_selected=='Video to text'):
    print('Đang xây dựng :v')
else:
    youtube2video(download_path,youtube_url)
In [0]:
 

Leave a Reply

Your email address will not be published. Required fields are marked *