08
2022
06

Youtube dowload to mp4 video/ mp3 song (Using Youtube_dl)

"""Download Youtube video to mp4"""

try:
  import youtube_dl

except:
  !pip install ffmpeg;
  !pip install youtube_dl;
  !apt-get update 
  !pip install opencc-python-reimplemented;
  import youtube_dl
  
import os
from google.colab import drive
from youtube_dl import YoutubeDL
from opencc import OpenCC
import time
 
drive.mount('/content/drive', force_remount=False)
 
path = '/content/drive/MyDrive/Mp4_folder/'

try:
  os.chdir(path)
except:
  os.makedirs(path)
 
cc = OpenCC('s2t')

def get_video_info_and_download(youtube_url):
    video_info = {}

    with youtube_dl.YoutubeDL() as ydl:
        info = ydl.extract_info(youtube_url, download=True)

        video_info['ID'] = info.get('id')
        video_info['標題'] = info.get('title')
        video_info['影片縮圖'] = info.get('thumbnail')
        video_info['上傳者'] = info.get('uploader')
        video_info['上傳者網址'] = info.get('uploader_url')
        video_info['影片長度(秒)'] = info.get('duration')
        video_info['觀看次數'] = info.get('view_count')
        video_info['留言數'] = info.get('comment_count'# -
        video_info['喜歡數'] = info.get('like_count')
        video_info['不喜歡數'] = info.get('dislike_count')
        video_info['平均評分'] = info.get('average_rating')
        video_info['描述'] = info.get('description')
        video_info['標籤'] = info.get('tags')
        video_info['網頁網址'] = info.get('webpage_url')
        video_info['上傳日期'] = info.get('upload_date')

    return video_info

url = input('請輸入url'':\n>>')
print(get_video_info_and_download(url))

for file_name in os.listdir(path):
    newName = cc.convert(file_name)

    if newName != file_name:
        os.rename(path+file_name, path+newName)
        print(file_name,'====>',newName)
                
print("success")

#=================================================================================================================================

"""Download Youtube video to mp3"""
try:
  import youtube_dl

except:
  !pip install ffmpeg;
  !pip install youtube_dl;
  !apt-get update 
  !pip install opencc-python-reimplemented;
  import youtube_dl
  
import os
from google.colab import drive
from youtube_dl import YoutubeDL
from opencc import OpenCC
import time
 
drive.mount('/content/drive', force_remount=False)
path = '/content/drive/MyDrive/Mp3_folder/'

try:
  os.chdir(path)
except:
  os.makedirs(path)
 
cc = OpenCC('s2t')

outtmpl = '{}%(title)s.%(ext)s'.format(path)

ydl_opts =  {
    'outtmpl': outtmpl,
    'noplaylist'False,
    'ignoreerrors'True,
    'quiet'False,
    'cachedir'False,
    'format''bestaudio/best',
    'nocheckcertificate':True,
    'postprocessors': [{
        'key''FFmpegExtractAudio',
        'preferredcodec''mp3',
        'preferredquality''320',
    }],
}

url = input('請輸入url'':\n>>')

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    try:
        ydl.cache.remove()
        ydl.download([url])
    except youtube_dl.DownloadError as error:
        print(error)

for file_name in os.listdir(path):
    newName = cc.convert(file_name)

    if newName != file_name:
        os.rename(path+file_name, path+newName)
        print(file_name,'====>',newName)
                
print("success")


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。