首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> 语言基础 >> 正文
调整 srt 字幕文件时间戳的程序
作者:小兵    文章来源:小兵的工具箱    更新时间:2007-9-3 9:44:19
/***************************************************************************
 * 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;
}
相关文章:
没有相关文章
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号