提取Youtube音频文本内容

本文介绍了如何通过Python的pytube、moviepy和whisper库实现从YouTube下载视频、提取音频并将其转换为文本的三个步骤。首先下载视频,然后使用moviepy分离音频并转为MP3格式,最后通过whisper的小型模型进行音频转文字处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现这个功能要分成三个步骤:下载Youtube文件,分离文件音频和音频转文本。

下载Youtube文件

 通过pytube模块下载Youtube文件。

from pytube import YouTube

def Download(link):
    youtubeObject = YouTube(link)
    youtubeObject = youtubeObject.streams.get_lowest_resolution()
    try:
        videoFilePath = youtubeObject.download()
        print("Download is completed successfully")
    except:
        print("An error has occurred")
    return videoFilePath

分离文件音频

通过moviepy模块提取文件的音频数据并转成mp3格式的文件。

import moviepy.editor as mp

from pathlib import Path

def VideoToAudio(videoFilePath):
    clip = mp.VideoFileClip(videoFilePath)
    audioFilePath = Path(videoFilePath).with_suffix(".mp3")
    clip.audio.write_audiofile(audioFilePath)
    if (Path(audioFilePath).exists()):
        return audioFilePath.as_posix()
    else:
        return ""

音频转文本

通过whisper模块加载small模型转换音频文件成文本。

import whisper

def AudioToText(audioFilePath):
    model = whisper.load_model("small")
    result = model.transcribe(audio=audioFilePath, task = 'translate')
    print(result["text"])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值