一種並行下載youtube視頻的方法

import argparse
import glob
import json
import os
import shutil
import subprocess
import uuid
from multiprocessing import Pool

def download_clip(info):
        video_id = info[0]
        uid = info[1]
        tmp_dir = "/tmp/youtube"
        num_attempts = 5
        url_base = "https://www.youtube.com/watch?v="
        output_dir = "./video"
        output_filename = os.path.join(output_dir, video_id + ".mp4")
        tmp_filename = os.path.join(tmp_dir, '%s.%%(ext)s' % uid)
        command = ['youtube-dl',
                           '--quiet', '--no-warnings',
                           '-f', 'mp4',
                           '-o', '"%s"' % tmp_filename,
                           '"%s"' % (url_base + video_id)]
        command = ' '.join(command)
        print (command)
        attempts = 0 
        while True:
                try:
                        output = subprocess.check_output(command, shell=True,                                                                                        stderr=subprocess.STDOUT)
                except subprocess.CalledProcessError as err:
                        attempts += 1
                        if attempts == num_attempts:
                                return err.output
                else:
                        break

        tmp_filename = glob.glob('%s*' % tmp_filename.split('.')[0])[0]
        # Construct command to trim the videos (ffmpeg required).
        command = ['ffmpeg',
                           '-i', '"%s"' % tmp_filename,
                           '-c:v', 'libx264', '-c:a', 'copy',
                           '-threads', '1',
                           '-loglevel', 'panic',
                           '"%s"' % output_filename]
        command = ' '.join(command)
        try:
                output = subprocess.check_output(command, shell=True,                                                                        stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as err:
                return err.output

        # Check if the video was successfully saved.
        status = os.path.exists(output_filename)
        os.remove(tmp_filename)

if __name__ == '__main__':

        videoid_uid = []

        #video_ids.txt文件中存放youtube視頻的videoId列表,每行一個,eg.ODoawH2AkJg
        with open("video_ids.txt") as lines:
                for line in lines:
                        line = line.strip()
                        uid = uuid.uuid4()
                        videoid_uid.append((line, uid))
        pool = Pool(16)
        pool.map(download_clip, videoid_uid)
        pool.close()
        pool.join()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章