利用python对视频字幕进行识别

import easyocr
import cv2 as cv
from PIL import Image
video_file = cv.VideoCapture(r"f4459201ee68667a36dee475fe96159c.mp4")
video_fps=video_file.get(cv.CAP_PROP_FPS)
print(video_file.get(cv.CAP_PROP_FPS))
total_frames = int(video_file.get(cv.CAP_PROP_FRAME_COUNT))
image_size = (int(video_file.get(cv.CAP_PROP_FRAME_HEIGHT)), int(video_file.get(cv.CAP_PROP_FRAME_WIDTH)))
frames_height,frames_weight=image_size[0],image_size[1]
print(frames_height)
count_frame_start=0
count_frame_end=0
thresh = 220#设定阈值 进行二值化
temporary_frame=[]
reader=easyocr.Reader(["ch_sim","en"],gpu=False)#用于识别文字
"""
下面函数分别计算是否有字幕的区别   字幕是否相同的
相当于从 无----->有   计算每张图像与 0 值图像的误差
return ((img - imgo) ** 2).sum() / img.size * 100 可以添加到代码中,然后变成属于你自己的代码
   有----->变化    接着计算相同字幕和不同字幕图像直接的误差
"""
def cal_video(img, imgo=None):
        return ((img - imgo) ** 2).sum() / img.size * 100

while True:
    success, frames = video_file.read()
    print("打开第{}帧".format(count_frame_start))
    frames_cut = frames[:, :, 0]#[(486, 864)]
    frames_wh_cut = frames_cut[frames_height-75: frames_height-6, :]
    _, frames_threshold = cv.threshold(frames_wh_cut, 220, 255, cv.THRESH_BINARY)
    temporary_frame.append(frames_threshold)
    if count_frame_start>1:
        del temporary_frame[0]

        if cal_video(temporary_frame[1],temporary_frame[0])>2:
            print("程序运行!第{}帧".format(count_frame_start))
            result = reader.readtext(frames_wh_cut)
            if len(result)>0:
                f=open("嫦娥奔月.txt", "a", encoding="utf-8")
                f.write(str(count_frame_end) + "--->" + str(count_frame_start)+"\n")
                f.write(str(result[0][1])+"\n")
                f.close()
                count_frame_end = count_frame_start
                count_frame_start+=1
                #cv.imwrite(r"D:\PycharmProjects\pythonProject\feiji\image{}.jpg".format(count_frame_start),frames_wh_cut)
            else:
                continue
        else:
            pass
    count_frame_start+=1

大家运行程序注意一下几点:

1.更改视频地址,选择你需要进行字幕识别的视频地址

2.对所截取的视频字幕图片进行二值化,其中阈值可以自己更改

3.相邻图片的相似值的阈值可以进行更改。

 总体来说,识别还是有一些问题的,大家将这个代码跑完之后就会发现问题所在,如果谁能够提升效果,希望告知,谢谢。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以使用一些库来提取视频字幕,以下是其中的一些方法: 1. 使用pydub库来提取音频文件,再使用SpeechRecognition库来识别并转换为文字。这个方法需要一个视频文件和其支持的音频格式。 ```python from pydub import AudioSegment import speech_recognition as sr video_path = 'path/to/video.mp4' audio = AudioSegment.from_file(video_path, format='mp4') audio.export("audio.wav", format="wav") r = sr.Recognizer() audio_file = sr.AudioFile('audio.wav') with audio_file as source: audio = r.record(source) text = r.recognize_google(audio, language='en-US') print(text) ``` 2. 使用pip install SpeechRecognition 和pip install moviepy 安装所需的一个库和Python package。该方法也是从视频中提取音频,然后使用moviepy获取其视频字幕。需要提供视频文件并安装所需的库。 ```python import speech_recognition as sr from moviepy.video.io.VideoFileClip import VideoFileClip r = sr.Recognizer() video_path = 'path/to/video.mp4' clip = VideoFileClip(video_path) # 提取音频 clip.audio.write_audiofile('audio.wav') sound = AudioSegment.from_file('audio.wav', format='wav') # 获取字幕 def transcribe_audio(sound): transcript = '' with sr.AudioFile(sound) as source: audio_text = r.record(source) try: transcript = r.recognize_google(audio_text) except sr.UnknownValueError as e: print(e) return transcript transcription = transcribe_audio('audio.wav') print(transcription) ``` 3. 使用OpenCV库来提取字幕Opencv库是一款面向计算机视觉的开源计算机视觉库。在这里它被用来获取视频字幕,并使用tesseract进行OCR处理。 ```python import cv2 import pytesseract video_path = 'path/to/video.mp4' vidcap = cv2.VideoCapture(video_path) frames = [] success, image = vidcap.read() count = 0 success = True while success: frames.append(image) success, image = vidcap.read() count += 1 for frame in frames: text = pytesseract.image_to_string(frame, lang='eng') print(text) ``` 这里只提供了一些用Python提取视频字幕的方法,您可以根据您自己的需求,选择其中的某一种方法来完成相应的任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值