调整 srt 字幕文件时间戳的程序


/***************************************************************************

 * Description:
 *           A tool to adjust the timestamps of srt subtitle files.
 *           Version 0.1
 * Author  : wplxb
 * Language: C
 * Date    : 2007-07-01
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_LINE_LEN 400
#define TIMESTAMP_LEN 27

void print_help(char * filename)
{
    printf("Usage: %s [Options] file/n", filename);
    printf("Options:/n");
    printf("  +sec     Add sec seconds to the subtitle file specified./n");
    printf("  -sec     Subtract sec seconds to the subtitle file specified./n");
    printf("  --help   Display this information./n");
    printf("  -h       Display this information./n");
    printf("  -?       Display this information./n");
    printf("/n");
    printf("Note that the adjusted subtitle will be outputed to the standard output./n");
}

void preprocess(int argc, char *argv[])
{
    char * filename = "timestamp";
    char * arg1;

    if (1 == argc)
    {
        print_help(filename);
        exit(1);
    }

    arg1 = argv[1];

    if (0 == strcmp("-h", arg1)
            || 0 == strcmp("--help", arg1)
            || 0 == strcmp("-?", arg1))
    {
        print_help(filename);
        exit(0);
    }
    else if (argc < 3)
    {
        print_help(filename);
        exit(1);
    }
    else if ('+' != arg1[0] && '-' != arg1[0])
    {
        print_help(filename);
        exit(1);
    }

    if (strspn(arg1 + 1, "0123456789") != strlen(arg1 + 1))
    {
        print_help(filename);
        exit(1);
    }
}

int is_timestamp_line(char * line)
{
    if (!line)
    {
        return 0;
    }

    line = strpbrk(line, "0123456789");
    if (line)
    {
        if (strlen(line) >= TIMESTAMP_LEN
                && ':' == line[2]
                && ':' == line[5]
                && ',' == line[8])
        {
            return 1;
        }
    }

    return 0;
}

int main(int argc, char * argv[])
{
    FILE * srt;
    int seconds;
    char timestamp_line[MAX_LINE_LEN];

    preprocess(argc, argv);

    seconds = atoi(argv[1] + 1);
    if ('-' == argv[1][0])
    {
        seconds = -seconds;
    }


    if (NULL == (srt = fopen(argv[2], "r")))
    {
        printf("Can't open file /"%s/"!/n", argv[2]);
        exit(errno);
    }

    /* The real process starts here. */
    while (fgets(timestamp_line, sizeof(timestamp_line), srt))
    {
        if (is_timestamp_line(timestamp_line))
        {
            int start_hour;
            int start_minute;
            int start_second;
            int start_millisecond;
            int start_time;
            int end_hour;
            int end_minute;
            int end_second;
            int end_millisecond;
            int end_time;

            sscanf(timestamp_line, "%d", &start_hour);
            sscanf(timestamp_line + 3, "%d", &start_minute);
            sscanf(timestamp_line + 6, "%d", &start_second);
            sscanf(timestamp_line + 9, "%d", &start_millisecond);
            start_time = start_hour;
            start_time = start_time * 60 + start_minute;
            start_time = start_time * 60 + start_second;
            start_time += seconds;
            start_second = start_time % 60;
            start_time /= 60;
            start_minute = start_time % 60;
            start_time /= 60;
            start_hour = start_time;

            sscanf(timestamp_line + 17, "%d", &end_hour);
            sscanf(timestamp_line + 20, "%d", &end_minute);
            sscanf(timestamp_line + 23, "%d", &end_second);
            sscanf(timestamp_line + 26, "%d", &end_millisecond);
            end_time = end_hour;
            end_time = end_time * 60 + end_minute;
            end_time = end_time * 60 + end_second;
            end_time += seconds;
            end_second = end_time % 60;
            end_time /= 60;
            end_minute = end_time % 60;
            end_time /= 60;
            end_hour = end_time;

            printf("%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d/n",
                    start_hour, start_minute, start_second, start_millisecond,
                    end_hour, end_minute, end_second, end_millisecond);
        }
        else
        {
            printf("%s", timestamp_line);
        }
    }

    fclose(srt);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将JSON换为SRT格式的字幕文件包括以下步骤: 1. 读取JSON文件并解析成字典格式。 2. 遍历字典,获取每个字幕的开始时间、结束时间和文本内容。 3. 将开始时间和结束时间换为SRT格式的时间戳,格式为:hh:mm:ss,msmsms,其中msmsms为毫秒数。 4. 按照SRT格式,将时间戳字幕内容(如果有换行符则需要替换为SRT格式的空格符)和空行依次写入SRT文件中。 下面是一个Python代码示例: ```python import json def json_to_srt(json_file, srt_file): with open(json_file, 'r') as f: data = json.load(f) with open(srt_file, 'w') as f: count = 1 for subtitle in data: start_time = format_time(subtitle['start_time']) end_time = format_time(subtitle['end_time']) text = subtitle['text'].replace('\n', '\r\n') f.write(f"{count}\n{start_time} --> {end_time}\n{text}\n\n") count += 1 def format_time(time): ms = int((time - int(time)) * 1000) time_str = time.strftime('%H:%M:%S') return f"{time_str},{ms:03}" # 示例:将json_file.json换为srt_file.srt json_to_srt('json_file.json', 'srt_file.srt') ``` 其中,`json_file`为输入的JSON文件路径,`srt_file`为输出的SRT文件路径。函数内部使用`json.load()`函数将JSON文件解析成字典对象,遍历字典对象,获取每个字幕的开始时间、结束时间和文本内容,然后调用`format_time()`函数将时间格式化为SRT格式的时间戳,最后将时间戳字幕内容和空行按照SRT格式写入输出文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值