ihmily / DouyinLiveRecorder

可循环值守和多人录制的直播录制软件,支持抖音、TikTok、Youtube、快手、虎牙、斗鱼、B站、小红书、pandatv、sooplive、flextv、popkontv、twitcasting、winktv、百度、微博、酷狗、17Live、Twitch、Acfun、CHZZK、shopee等40+平台直播录制
MIT License
5.11k stars 629 forks source link

❓[Question] 录屏怎么生成时间字幕文件,有偿V50帮帮忙可以吗 谢谢大佬 #756

Open allforfreedom opened 3 weeks ago

allforfreedom commented 3 weeks ago

⚠️ 搜索是否存在类似问题

🔧 运行方式

直接运行的exe文件

🐍 如果是使用源代码运行,请选择你的Python环境版本

Python 3.13

💻 请选择你的系统环境

Windows 10

🤔 问题详情

生成时间字幕文件 知道开始时间、视频时长、怎么生成srt的字幕文件。 场景:用OBS录制直播生成的MP4文件,文件名有开始时间。 微信截图_20241112220137

Issues-translate-bot commented 3 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


Title: ❓[Question] Can I generate a time subtitle file with a paid V50? Thank you, sir.

⚠️ Search for similar problems

🔧 How to run

exe file to run directly

🐍 If you are running from source code, please select your Python environment version

Python 3.13

💻 Please select your system environment

Windows 10

🤔 Question details

Generate time subtitle file Know the start time, video duration, and how to generate srt subtitle files. Scenario: Use OBS to record the MP4 file generated by live broadcast. The file name has a start time.

gyc0123 commented 3 weeks ago

什么平台

Issues-translate-bot commented 3 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


What platform

allforfreedom commented 2 weeks ago

什么平台 就是抖音,用OBS录屏生成的.MP4文件,文件名就是开始录制的时间,一样想生成个时间字幕 微信截图_20241112220137

gyc0123 commented 2 weeks ago

抖音没字幕功能吧,要是视频语音转文字或者srt,可以用科大讯飞的ai

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


Douyin doesn’t have a subtitle function. If you want to convert video speech to text or SRT, you can use iFlytek’s AI

allforfreedom commented 2 weeks ago

抖音没字幕功能吧,要是视频语音转文字或者srt,可以用科大讯飞的ai 不是实时说话的字幕,只是看个时间 这个项目有,但是我不会 哈哈 微信截图_20241113154849

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


How about letting chatgpt write one, read the time from the file name, and then add one line of time subtitles every minute. Adjust the datetime library string to convert the time, and then loop the output time in srt format. Here is chatgpt's answer:

To add time subtitles to your videos, you can do it using some automated scripts or tools. The exact steps depend on your operating system and the tools you use. The following is a rough process using ffmpeg and Python scripts to generate time subtitles. ffmpeg is a powerful video processing tool that can be used to add subtitles and perform other video editing operations.

1. Install necessary tools

First, make sure you have the following tools installed:

You can install them with the following command:

2. Python script to generate subtitle files

Next, we can write a Python script that will read the timestamp of the video file and generate a subtitle file in .srt format. Insert a subtitle every minute, showing the current time of the video.

Sample Python script:

import os
import datetime

# Enter the video file path
video_filename = "your_video_file.mp4"

# Get the duration of the video
import subprocess

def get_video_duration(filename):
    result = subprocess.run(
        ['ffmpeg', '-i', filename, '-v', 'quiet', '-print_format', 'json', '-show_format'],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    output = result.stderr.decode()
    duration_str = [line for line in output.splitlines() if "duration" in line][0]
    duration_seconds = float(duration_str.split('=')[1])
    return duration_seconds

# Generate subtitle file
def create_subtitles(video_filename):
    duration = get_video_duration(video_filename)
    subtitle_filename = video_filename.rsplit('.', 1)[0] + '.srt'

    with open(subtitle_filename, 'w') as f:
        subtitle_index = 1
        current_time = 0
        while current_time < duration:
            start_time = str(datetime.timedelta(seconds=current_time))
            end_time = str(datetime.timedelta(seconds=current_time + 60))
            # Format to srt time format (hh:mm:ss,SSS --> hh:mm:ss,SSS)
            start_time = start_time[:-3].replace('.', ',') # Delete. and format
            end_time = end_time[:-3].replace('.', ',')

            # Time subtitle content
            subtitle_text = start_time
            f.write(f"{subtitle_index}\n")
            f.write(f"{start_time} --> {end_time}\n")
            f.write(f"{subtitle_text}\n\n")

            subtitle_index += 1
            current_time += 60

    print(f"Subtitle file has been generated: {subtitle_filename}")

create_subtitles(video_filename)

3. Add subtitles to video

After generating subtitle files, the next step is to add these subtitle files to the video. Subtitles can be embedded into videos using the ffmpeg command.

Use ffmpeg to add subtitles:

ffmpeg -i input_video.mp4 -i subtitles.srt -c:v copy -c:a copy -c:s mov_text output_video.mp4

4. Automated operations

If you have multiple files to process, you can modify the script to loop through all .mp4 files in a directory and process them in batches:

import glob

def process_videos_in_directory(directory):
    video_files = glob.glob(os.path.join(directory, "*.mp4"))
    for video_filename in video_files:
        create_subtitles(video_filename)
        print(f"Processed: {video_filename}")

#Set the directory where the video file is located
video_directory = "path/to/your/videos"
process_videos_in_directory(video_directory)

In this way, the script will automatically process all .mp4 files in the specified directory, generate and add time subtitles.

Summary

In this way, you can add time subtitles to videos in batches. If you have other specific needs or want to optimize the script further, feel free to let me know!

gyc0123 commented 2 weeks ago

config/config.ini里面有个生成时间字幕文件 = 否 把否改成是


这个机器人好尴尬啊,我说错了都删不掉

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


There is a Generate time subtitle file = No in config/config.ini Change no to yes


This robot is so embarrassing. I can’t delete it even if I say something wrong.

allforfreedom commented 2 weeks ago

config/config.ini里面有个 把否改成是生成时间字幕文件 = 否

这个机器人好尴尬啊,我说错了都删不掉

是的,这个项目有这个生成字幕功能。所以我想借鉴这个功能,使用到类似的 2024-11-12 23-39-45.mp4 文件上。用obs录制出来的MP4文件,文件名是开始时间,读取视频时长,生成一样时间长度的时间字幕文件。 微信截图_20241113194331

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


There is a question in config/config.ini. Change No to Yes Generate time subtitle file = No

This robot is so embarrassing. I can’t delete it even if I say something wrong.

Yes, this project has this subtitle generation function. So I want to borrow this feature and use it on a similar 2024-11-12 23-39-45.mp4 file. For MP4 files recorded with obs, the file name is the start time. Read the video duration and generate a time subtitle file of the same length. WeChat screenshot_20241113194331

gyc0123 commented 2 weeks ago

这是那段代码,while循环输出一行字幕sleep 1秒,你也可以for循环秒数 main.py

def generate_subtitles(record_name: str, ass_filename: str, sub_format: str = 'srt') -> None:
    index_time = 0
    today = datetime.datetime.now()
    re_datatime = today.strftime('%Y-%m-%d %H:%M:%S')

    def transform_int_to_time(seconds: int) -> str:
        m, s = divmod(seconds, 60)
        h, m = divmod(m, 60)
        return f"{h:02d}:{m:02d}:{s:02d}"

    while True:
        index_time += 1
        txt = str(index_time) + "\n" + transform_int_to_time(index_time) + ',000 --> ' + transform_int_to_time(
            index_time + 1) + ',000' + "\n" + str(re_datatime) + "\n\n"

        with open(f"{ass_filename}.{sub_format.lower()}", 'a', encoding=text_encoding) as f:
            f.write(txt)

        if record_name not in recording:
            return
        time.sleep(1)
        today = datetime.datetime.now()
        re_datatime = today.strftime('%Y-%m-%d %H:%M:%S')
Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


This is the code. The while loop outputs a line of subtitles and sleeps for 1 second. You can also use the for loop to count the seconds. main.py


def generate_subtitles(record_name: str, ass_filename: str, sub_format: str = 'srt') -> None:
index_time = 0
today = datetime.datetime.now()
re_datatime = today.strftime('%Y-%m-%d %H:%M:%S')
def transform_int_to_time(seconds: int) -> str:
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    return f"{h:02d}:{m:02d}:{s:02d}"

while True:
    index_time += 1
    txt = str(index_time) + "\n" + transform_int_to_time(index_time) + ',000 --> ' + transform_int_to_time(
        index_time + 1) + ',000' + "\n" + str(re_datatime) + "\n\n"

    with open(f"{ass_filename}.{sub_format.lower()}", 'a', encoding=text_encoding) as f:
        f.write(txt)

    if record_name not in recording:
        return
    time.sleep(1)
    today = datetime.datetime.now()
    re_datatime = today.strftime('%Y-%m-%d %H:%M:%S')