File size: 1,473 Bytes
c83dd81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
'''

pip install youtube-dl==2020.12.12

'''
import subprocess
import pandas as pd

def download_youtube_video(video_url, output_path):
    """

    :param video_url: youtube video url

    :param output_dir: file path to save

    """
    # video_url, output_path = info
    try:
        if not os.path.exists(os.path.dirname(output_path)):
            os.makedirs(os.path.dirname(output_path), exist_ok=True)
        # download command
        command = ['yt-dlp', '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]', '--merge-output-format',
               'mp4', '--output', output_path , video_url]
        # subprocess.run
        result = subprocess.run(command, capture_output=True, text=True, encoding='utf-8')

        if result.returncode == 0:
            print('Download {:s} successfully!'.format(video_url))
        else:
            print("Fail to download {:s}, error info:\n{:s}".format(video_url, result.stderr))
    except Exception as e:
        print(f"error: {e}")

if __name__ == '__main__':
    df = pd.read_csv("./echomimicv2_benchmark_url+start_timecode+end_timecode.txt")
    save_dir = "ori_video_dir"
    urls = list(set(df['URL']))
    video_output_paths = [os.path.join(save_dir, url.split('v=')[1]+".mp4") for url in urls]

    for video_url, output_path in zip(urls, video_output_paths):
        download_youtube_video(video_url, output_path)