python 视频提取字幕

目录

ffmpeg提取字幕 没测试成功

opencv 加ocr


ffmpeg提取字幕 没测试成功
 

ffmpeg -i input_video.mp4 -map 0:s:0 output_subtitle.srt 

import subprocess

from moviepy.editor import VideoFileClip

video = VideoFileClip('video_o.mp4')


# subprocess.run(['ffmpeg', '-i', 'video_o.mp4', '-map', '0', '-c:s', 'srt','subtitles.srt'])

subprocess.run(['ffmpeg', '-i', 'video_o.mp4', '-map', '0:s:0','subtitles.srt'])

opencv 加ocr

import cv2
import pytesseract

# 加载视频
video_path = 'path/to/video.mp4'
video = cv2.VideoCapture(video_path)

# 设置字幕文件保存路径
output_file = 'path/to/subtitles.srt'
subtitles = []

# 逐帧处理视频
while True:
    ret, frame = video.read()
    if not ret:
        break
    
    # 将帧转换为灰度图像
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # 使用Tesseract提取文本
    text = pytesseract.image_to_string(gray)
    
    # 获取当前帧的时间戳
    timestamp = video.get(cv2.CAP_PROP_POS_MSEC)
    
    # 将文本和时间戳添加到字幕列表中
    subtitles.append((timestamp, text))

# 关闭视频流
video.release()

# 将字幕保存为SRT文件
with open(output_file, 'w') as f:
    for i, (timestamp, text) in enumerate(subtitles):
        start_time = int(timestamp)
        end_time = int(timestamp) + 1000  # 假设每帧持续1秒钟
        f.write(f'{i+1}\n')
        f.write(f'{start_time} --> {end_time}\n')
        f.write(f'{text}\n\n')


https://blog.51cto.com/u_16213402/7617702

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值