Ejemplo n.º 1
0
def download_subtitle(video_id):
    """
    下载video的中英字幕并保存到video model中

    :param video_id:
    :return:
    """
    video = Video.objects.get(video_id=video_id)
    options = {
        'outtmpl': YOUTUBE_DOWNLOAD_DIR + '\%(title)s-%(id)s.%(ext)s',
        # name the file the ID of the video
        'verbose': True,  # Print various debugging information
        'restrictfilenames': True,
        'subtitleslangs': ['en', 'zh-Hans'],  # 要写成list的形式
        # 'convertsubtitles': 'srt',
        'subtitlesformat': 'vtt',
        'writeautomaticsub': True,  # 下载字幕,这里的字幕是youtube自动生成的CC字幕
        'skip_download': True,
    }

    with youtube_dl.YoutubeDL(options) as ydl:
        ydl.download([video.youtube_url])

        # 只适用于subtitlesformat设置为srt或ass的情况,设置为best则失效
        # 字幕名称格式 LG K10 and K7 hands-on-_9coAtC2PZI.en.srt
        subtitle_en_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                      keyword=video.video_id
                                                              + ".en",
                                                      extend=options.get(
                                                          'subtitlesformat',
                                                          None))

        result = []
        if (subtitle_en_filepath.__len__()) == 1:
            # 从list中把唯一的一个数据pop出来
            video.subtitle_en = subtitle_en_filepath.pop()
            result.append(video.subtitle_en)

        subtitle_cn_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                      keyword=video.video_id
                                                              + ".zh-Hans",
                                                      extend=options.get(
                                                          'subtitlesformat',
                                                          None))
        if (subtitle_cn_filepath.__len__()) == 1:
            # 从list中把唯一的一个数据pop出来
            video.subtitle_cn = subtitle_cn_filepath.pop()
            result.append(video.subtitle_en)

        video.save()

    if result == []:
        result = False
    return result
Ejemplo n.º 2
0
def download_subtitle(video_id):
    """
    下载video的中英字幕并保存到video model中

    :param video_id:
    :return:
    """
    video = Video.objects.get(video_id=video_id)
    options = {
        'outtmpl': YOUTUBE_DOWNLOAD_DIR + '\%(title)s-%(id)s.%(ext)s',
        # name the file the ID of the video
        'verbose': True,  # Print various debugging information
        'restrictfilenames': True,
        'subtitleslangs': ['en', 'zh-Hans'],  # 要写成list的形式
        # 'convertsubtitles': 'srt',
        'subtitlesformat': 'vtt',
        'writeautomaticsub': True,  # 下载字幕,这里的字幕是youtube自动生成的CC字幕
        'skip_download': True,
    }

    with youtube_dl.YoutubeDL(options) as ydl:
        ydl.download([video.youtube_url])

        # 只适用于subtitlesformat设置为srt或ass的情况,设置为best则失效
        # 字幕名称格式 LG K10 and K7 hands-on-_9coAtC2PZI.en.srt
        subtitle_en_filepath = search_keyword_in_file(
            dir=YOUTUBE_DOWNLOAD_DIR,
            keyword=video.video_id + ".en",
            extend=options.get('subtitlesformat', None))

        result = []
        if (subtitle_en_filepath.__len__()) == 1:
            # 从list中把唯一的一个数据pop出来
            video.subtitle_en = subtitle_en_filepath.pop()
            result.append(video.subtitle_en)

        subtitle_cn_filepath = search_keyword_in_file(
            dir=YOUTUBE_DOWNLOAD_DIR,
            keyword=video.video_id + ".zh-Hans",
            extend=options.get('subtitlesformat', None))
        if (subtitle_cn_filepath.__len__()) == 1:
            # 从list中把唯一的一个数据pop出来
            video.subtitle_cn = subtitle_cn_filepath.pop()
            result.append(video.subtitle_en)

        video.save()

    if result == []:
        result = False
    return result
Ejemplo n.º 3
0
def search_subtitles_file(video_id, subtitle_format):
    """
    要自己查看下载目录下是否有相关video id的字幕文件
    入存在并将视频文件的地址保存到对应的字段
    :param video_id:
    :param subtitle_format:vtt, ass
    :return:
    """

    # 只能查找到一个这样的文件才对
    # if (filepath.__len__()) == 1:
    #     # 从list中把唯一的一个数据pop出来
    #     subtitle_filepath = filepath.pop()
    #     return subtitle_filepath
    # else:
    #     return False

    video = Video.objects.get(video_id=video_id)
    subtitle_en_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                  keyword=video_id +
                                                          ".en",
                                                  extend=subtitle_format)

    result = []
    if (subtitle_en_filepath.__len__()) > 0:
        # 从list中把唯一的一个数据pop出来
        video.subtitle_en = subtitle_en_filepath.pop()
        result.append(video.subtitle_en.path)

    subtitle_cn_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                  keyword=video_id
                                                          + ".zh-Hans",
                                                  extend=subtitle_format)
    if (subtitle_cn_filepath.__len__()) > 0:
        # 从list中把唯一的一个数据pop出来
        video.subtitle_cn = subtitle_cn_filepath.pop()
        result.append(video.subtitle_cn.path)

    video.save(update_fields=["subtitle_en", "subtitle_cn"])

    if result == []:
        result = False
    else:
        return result
Ejemplo n.º 4
0
def search_subtitles_file(video_id, subtitle_format):
    """
    要自己查看下载目录下是否有相关video id的字幕文件
    入存在并将视频文件的地址保存到对应的字段
    :param video_id:
    :param subtitle_format:vtt, ass
    :return:
    """

    # 只能查找到一个这样的文件才对
    # if (filepath.__len__()) == 1:
    #     # 从list中把唯一的一个数据pop出来
    #     subtitle_filepath = filepath.pop()
    #     return subtitle_filepath
    # else:
    #     return False

    video = Video.objects.get(video_id=video_id)
    subtitle_en_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                  keyword=video_id + ".en",
                                                  extend=subtitle_format)

    result = []
    if (subtitle_en_filepath.__len__()) > 0:
        # 从list中把唯一的一个数据pop出来
        video.subtitle_en = subtitle_en_filepath.pop()
        result.append(video.subtitle_en.path)

    subtitle_cn_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                  keyword=video_id +
                                                  ".zh-Hans",
                                                  extend=subtitle_format)
    if (subtitle_cn_filepath.__len__()) > 0:
        # 从list中把唯一的一个数据pop出来
        video.subtitle_cn = subtitle_cn_filepath.pop()
        result.append(video.subtitle_cn.path)

    video.save(update_fields=["subtitle_en", "subtitle_cn"])

    if result == []:
        return False
    else:
        return result
Ejemplo n.º 5
0
def search_video_file(video_id, file_extend):
    # 要自己查看下载目录下是否有相关video id的视频、字幕文件
    # 入存在并将视频文件的地址保存到对应的字段
    video = Video.objects.get(video_id=video_id)

    video_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                            keyword=video.video_id,
                                            extend=file_extend)
    # 只能查找到一个这样的文件才对
    if (video_filepath.__len__()) == 1:
        # 从list中把唯一的一个数据pop出来
        video.file = video_filepath.pop()
        video.save()
        return video_filepath
    else:
        # 找到多个文件,暂时返回False
        return False
Ejemplo n.º 6
0
def download_single_youtube_video_main(video_id):
    """
    下载单个youtube视频,并将下载后的视频文件的目录保存到Video.file
    :param video_id:
    :return:
    """
    video = Video.objects.get(video_id=video_id)

    # 代码参考 https://github.com/rg3/youtube-dl/blob/master/README.md#embedding
    # -youtube-dl
    # 参数 https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL
    # .py#L121-L269
    # 参考 http://willdrevo.com/downloading-youtube-and-soundcloud-audio-with
    # -python-and-pandas/
    # 支持参数列表 https://github.com/rg3/youtube-dl/blob/master/youtube_dl/options.py
    options = {
        # 'format': '160+250',  # 质量最低的视频,可以节约带宽,用于测试
        # 'format': '137+140',  # 质量最高的视频,且上传到优酷有声音

        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
        # 'format': 'bestvideo+bestaudio/best',
        # 'extractaudio': True,  # only keep the audio
        # 'audioformat': "mp3",  # convert to mp3
        'outtmpl': YOUTUBE_DOWNLOAD_DIR + '\%(title)s-%(id)s.%(ext)s',
        # name the file the ID of the video
        'restrictfilenames': True,
        'noplaylist': True,  # only download single song, not playlist
        'verbose': True,  # Print various debugging information
        # 'subtitleslangs': ['zh-Hans', 'en'],  # 要写成list的形式
        # 'subtitlesformat': 'srt',
        # 'writeautomaticsub': True,  # 下载字幕,这里的字幕是youtube自动生成的CC字幕
        # 'embedsubtitles': False,  # Embed subtitles in the video (only for
        # mkv and mp4 videos
        'merge_output_format': 'mkv',
        'prefer_ffmpeg': True,
        'ffmpeg_location': "E:\\Program Files\\ffmpeg\\bin"
        # 'progress_hooks': [my_hook],
    }

    with youtube_dl.YoutubeDL(options) as ydl:
        # youtube_url = video.youtube_url
        # 用设置成list的形式
        ydl.download([video.youtube_url])

        # youtube-dl下载成功后并不会返回下载视频文件的信息
        # todo 所以要自己查看下载目录下是否有相关video id的视频,以此来判断是否下载成功
        # 并将视频文件的地址保存到对应的字段
        video_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                keyword=video.video_id,
                                                extend=options.get(
                                                    'merge_output_format',
                                                    None))
        # 只能查找到一个这样的文件才对
        if (video_filepath.__len__()) == 1:
            # 从list中把唯一的一个数据pop出来
            video.file = video_filepath.pop()

        # 只适用于subtitlesformat设置为srt或ass的情况,设置为best则失效
        # 字幕名称格式 LG K10 and K7 hands-on-_9coAtC2PZI.en.srt
        # subtitle_en_filepath = search_keyword_in_file(
        # dir=YOUTUBE_DOWNLOAD_DIR,
        #                                               keyword=video.video_id
        #                                                       + ".en",
        #                                               extend=options.get(
        #                                                   'subtitlesformat',
        #                                                   None))

        video.save()
    return video_filepath
Ejemplo n.º 7
0
def download_single_youtube_video_main(video_id):
    """
    下载单个youtube视频,并将下载后的视频文件的目录保存到Video.file
    :param video_id:
    :return:
    """
    video = Video.objects.get(video_id=video_id)

    # 代码参考 https://github.com/rg3/youtube-dl/blob/master/README.md#embedding
    # -youtube-dl
    # 参数 https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL
    # .py#L121-L269
    # 参考 http://willdrevo.com/downloading-youtube-and-soundcloud-audio-with
    # -python-and-pandas/
    # 支持参数列表 https://github.com/rg3/youtube-dl/blob/master/youtube_dl/options.py
    options = {
        # 'format': '160+250',  # 质量最低的视频,可以节约带宽,用于测试
        # 'format': '137+140',  # 质量最高的视频,且上传到优酷有声音
        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
        # 'format': 'bestvideo+bestaudio/best',
        # 'extractaudio': True,  # only keep the audio
        # 'audioformat': "mp3",  # convert to mp3
        'outtmpl': YOUTUBE_DOWNLOAD_DIR + '\%(title)s-%(id)s.%(ext)s',
        # name the file the ID of the video
        'restrictfilenames': True,
        'noplaylist': True,  # only download single song, not playlist
        'verbose': True,  # Print various debugging information
        # 'subtitleslangs': ['zh-Hans', 'en'],  # 要写成list的形式
        # 'subtitlesformat': 'srt',
        # 'writeautomaticsub': True,  # 下载字幕,这里的字幕是youtube自动生成的CC字幕
        # 'embedsubtitles': False,  # Embed subtitles in the video (only for
        # mkv and mp4 videos
        'merge_output_format': 'mkv',
        'prefer_ffmpeg': True,
        'ffmpeg_location': "E:\\Program Files\\ffmpeg\\bin"
        # 'progress_hooks': [my_hook],
    }

    with youtube_dl.YoutubeDL(options) as ydl:
        # youtube_url = video.youtube_url
        # 用设置成list的形式
        ydl.download([video.youtube_url])

        # youtube-dl下载成功后并不会返回下载视频文件的信息
        # todo 所以要自己查看下载目录下是否有相关video id的视频,以此来判断是否下载成功
        # 并将视频文件的地址保存到对应的字段
        video_filepath = search_keyword_in_file(dir=YOUTUBE_DOWNLOAD_DIR,
                                                keyword=video.video_id,
                                                extend=options.get(
                                                    'merge_output_format',
                                                    None))
        # 只能查找到一个这样的文件才对
        if (video_filepath.__len__()) == 1:
            # 从list中把唯一的一个数据pop出来
            video.file = video_filepath.pop()

        # 只适用于subtitlesformat设置为srt或ass的情况,设置为best则失效
        # 字幕名称格式 LG K10 and K7 hands-on-_9coAtC2PZI.en.srt
        # subtitle_en_filepath = search_keyword_in_file(
        # dir=YOUTUBE_DOWNLOAD_DIR,
        #                                               keyword=video.video_id
        #                                                       + ".en",
        #                                               extend=options.get(
        #                                                   'subtitlesformat',
        #                                                   None))

        video.save()
    return video_filepath