第八节课 字幕提取

1、/usr/local/Cellar/tesseract/4.1.1/share/tessdata

2、pip3 install pytesseract

export TESSDATA_PREFIX=/usr/local/Cellar/tesseract/4.1.1/share/tessdata
export PATH=$PATH:$TESSDATA_PREFIX

字幕识别

import pytesseract
import cv2
import numpy as np
from scipy import stats
import os
import matplotlib.pyplot as plt
 
 
if __name__=='__main__':
    path="20210701.mp4"
    print(path)
    cap=cv2.VideoCapture(path)
    frame_count=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print(frame_count)
    i=0
    while i<frame_count:
        cap.set(cv2.CAP_PROP_POS_FRAMES, i)
        _, frame = cap.read(i)
        if i==48:
            cv2.imwrite('20210701.jpg',frame)
        shape = frame.shape
        print(shape)
        #cv2.imshow("Frame-1", frame[580:630, 10:1270])
        #plt.imshow(frame)
        #plt.axis("off")
        #plt.show()
        img=frame[630:680, 50:1270]
        plt.imshow(img)
        plt.axis("off")
        plt.show()
        i=i+24*2
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        cv2.imshow("Frame-2:Gray", img)
        _,img=cv2.threshold(img,220,225,cv2.THRESH_BINARY)
        tessdata_dir_config='--tessdata-dir "/usr/local/Cellar/tesseract/4.1.1/share/tessdata" --psm 7 -c preserve_interword_spaces=1'
        word=pytesseract.image_to_string(img,lang='chi_sim',config=tessdata_dir_config)
        print(word)
        
        if cv2.waitKey(10)&0xff==ord("q"):
            break
        
    cap.release()
    cv2.destroyAllWindows()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python 实现 PC端剪映字幕转换SRT格式工具代码-Python 实现,# -*- coding: utf-8 -*- import getpass import os import json import re def get_time(time_int): # 使用正则表达式处理时间格式化问题 if time_int == 0: return '00:00:00,000' p = re.compile(r'(\d*)(\d{3})\d{3}') pl = p.findall(str(time_int))[0] if pl[0] == '': hms = '00:00:00' else: h = 0 m = 0 s = int(pl[0]) while s >= 60: m += 1 s -= 60 while m >= 60: h += 1 m -= 60 while h >= 24: exit('暂不支持超过24小时的字幕文件转换') hms = ':'.join((str(h).zfill(2), str(m).zfill(2), str(s).zfill(2))) return ','.join((hms, pl[1])) def format_time(start, end): # 拼接时间格式化后的字符串 return ' --> '.join((get_time(start), get_time(end))) def main(): # 取得电脑的用户名 username = getpass.getuser() # 拼接取得json文件夹所在地址 json_root_path = 'C:/Users/' + username + '/AppData/Local/JianyingPro/User Data/Projects/com.lveditor.draft/' # 拿到最后一次打开的json文件(内含字幕信息) if os.path.exists(json_root_path): with open(os.path.join(json_root_path, 'root_draft_meta_info.json'), 'r', encoding='utf-8') as f: json_path = (json.load(f)['all_draft_store'][0]['draft_fold_path']) # 打开json文件并将其转换为srt文件 if os.path.exists(json_path): with open(os.path.join(json_path, 'draft_content.json'), 'r', encoding='utf-8') as f: j = json.load(f) l1 = [] l2 = [] for i in j['tracks'][1]['segments']: start_time = int(i['target_timerange']['start']) end_time = int(i['target_timerange']['start'] + i['target_timerange']['duration']) l1.append(format_time(start_time, end_time)) for i in j['materials']['texts']: l2.append(i['content']) idx = 0 # 可以在此处自定义新建的srt字幕路径及文件名 with open('测试.srt', 'w', encoding='utf-8') as srt: while idx < len(l1):

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值