Giter Club home page Giter Club logo

Comments (5)

dinghaol avatar dinghaol commented on May 21, 2024

这是我的代码希望对你有所帮助

import json

import requests
from moviepy.video.io.VideoFileClip import VideoFileClip

from tool.file.fileBasic import listFileSort

import os
import shutil

from tool.file.txt.TxtWriter import BasicWriter


def videoToAudio(file):
    # 请求地址
    url = "http://127.0.0.1:9977/api"
    # 请求参数  file:音视频文件,language:语言代码,model:模型,response_format:text|json|srt
    # 返回 code==0 成功,其他失败,msg==成功为ok,其他失败原因,data=识别后返回文字
    files = {"file": open(file, "rb")}
    data = {"language": "zh", "model": "medium", "response_format": "text"}
    response = requests.request("POST", url, timeout=600, data=data, files=files)
    text = json.loads(response.text)["data"]
    return bytes(text, 'unicode_escape').decode('unicode_escape')


def split_video(input_path, output_prefix, segment_duration):
    # 打开视频文件
    video_clip = VideoFileClip(input_path)

    # 获取视频总时长
    total_duration = video_clip.duration

    # 计算分割点
    split_points = [t for t in range(0, int(total_duration), segment_duration)]

    # 分割视频
    for i, start_time in enumerate(split_points):
        end_time = min(start_time + segment_duration, total_duration)
        subclip = video_clip.subclip(start_time, end_time)
        subclip.write_videofile(f"{output_prefix}_part_{i + 1}.mp4")

    # 关闭视频文件
    video_clip.close()


def delete_all_files_in_folder(folder_path):
    # 遍历文件夹下的所有文件和子文件夹
    for file_name in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file_name)

        # 如果是文件,则删除
        if os.path.isfile(file_path):
            os.remove(file_path)
            print(f"Deleted file: {file_path}")

        # 如果是子文件夹,则递归调用删除
        elif os.path.isdir(file_path):
            shutil.rmtree(file_path)
            print(f"Deleted folder: {file_path}")


if __name__ == '__main__':
    path = r"G:\temp"
    files = listFileSort(path)
    for file in files:
        if os.path.isfile(file):
            temp = file.replace("\\", "/")
            split_video(file, path + r"\temp\temp", 300)
            text = ''
            for video in os.listdir(path + r"\temp"):
                temp = videoToAudio(path + r"\temp\\" + video)
                text = text + temp
                print(temp)
            BasicWriter(file.replace("mp4", "txt"), text, "w")
            delete_all_files_in_folder(path + r"\temp")
            delete_all_files_in_folder(r"F:\software\videoToText\sttv-0.91\static\tmp")

from stt.

jianchang512 avatar jianchang512 commented on May 21, 2024

内部已使用VAD自动按静音分割,应不会存在这个问题
只是vad消耗资源较多,如果视频很大而资源不足,可能出问题

from stt.

ZhongQiyu avatar ZhongQiyu commented on May 21, 2024

10分钟的效果会比5分钟,或者30分钟要好吗,还是其他的说法?

from stt.

lwstudy avatar lwstudy commented on May 21, 2024

还想说将英文电影识别翻译字幕来着,能支持到多大时长的?

from stt.

jianchang512 avatar jianchang512 commented on May 21, 2024

只要内存足够、显存足够,多大都可以。

无法读取是资源不足。尤其是使用 large大模型时,显示至少要8G以上

from stt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.