C语言提取srt字幕

设置srt字幕的结构体

typedef struct {
	char number[5];
	char count_time[100];
	char text[100];
} SrtSubtitle;  //src字幕的结构体
SrtSubtitle title[Storage];//存储字幕
typedef struct
{
	int start_time;
	int end_time;
}srctime;        //用于存储还未转换的时间结构--可后期其他方式进行调用
srctime sctime[Storage];//存储开始和结束时间

typedef struct
{
	int start_sc;
	int end_sc;
}millisecond;   //转换后的毫秒数
millisecond milsecond[Storage];//存储时间的毫秒

提取字幕时间和文本

static int subtitle(const string music_name)//获取字幕//"D:\\source\\subtitle\\Sign - DEAMN.srt"
{    
	string prefix = "D:\\source\\subtitle\\";
	string suffix = ".srt";
	string subtitle_music_name = prefix + music_name + suffix;
	//该位置重写需要进行传参--为文件的名字
	printf("subtitle中的地址为:%s", subtitle_music_name.c_str());
	FILE* fp = fopen(subtitle_music_name.c_str(), "r");//打开文件只读模式文件格式为anci"D:\\source\\subtitle\\我只在乎你邓丽君.srt"
	if (!fp)
	{
		printf("打开文件失败,查看地址\n");
		return 0;
	}
	char spacing[100];  //缓冲另一行空格
	int file_count = 0;  //该文件所有字幕个数
	while (fgets(title[file_count].number, 5, fp) != NULL)  //从文件中开始读取字符并保存
	{
		fgets(title[file_count].count_time, 100, fp);
		fgets(title[file_count].text, 100, fp);
		fgets(spacing, 100, fp);
		printf("nub:%s \ntime:%s\ntext:%s\n", title[file_count].number, title[file_count].count_time, title[file_count].text);  //获取src中的数字和时间和字幕
		file_count++;
	}
	fclose(fp);
	int count = 0;
	while (count < file_count)    //分割时间操作
	{
			string total_time = title[count].count_time;  //将char转为string型
				string subtime;   //接收纯0000230232023时间
	
				cout << "total_time:" << total_time << endl;

				for (int j = 0; j < total_time.length(); j++)
				{
					if (total_time[j] >= '0' && total_time[j] <= '9')  //将纯数字提取出来
					{
						subtime += total_time[j];
					}
				}
				cout << "suntime" << subtime << endl;
				string s_time=subtime.substr(0,9), e_time = subtime.substr(9, 9);//截取字符串
				cout << "截取字符start:   " << s_time << "   截取字符end:" << e_time << endl;
				sctime[count].start_time = atoi(s_time.c_str());//将字符串变为整型
				sctime[count].end_time = atoi(e_time.c_str());
				printf("转换start:%d   转换end:%d\n", sctime[count].start_time, sctime[count].end_time);
				milsecond[count].start_sc =deal_time(sctime[count].start_time);
				milsecond[count].end_sc = deal_time(sctime[count].end_time);//可能传参或者函数的调用出现问题
				printf("函数转换milsecongtart:%d,函数转换milsecondend:%d\n", milsecond[count].start_sc, milsecond[count].end_sc);
				count++;
	}
	return file_count;
}

处理时间——将字符串的时间变成毫秒

static int deal_time(int rtime)//时间处理函数,将时间转换位毫秒
{
	printf("传参的timedeal_time:%d\n", rtime);   //00001,21,980时间=1分钟21秒98==121980
	int milsecond;
	int min = (int)(rtime / 100000);//获取分钟数1
	int temp = rtime - min * 100000;//获取除了分钟的秒数的数字==21,980
	int secd =(int)(temp / 1000);//21
	int mils = temp - secd * 1000;//
	//milsecond = (min * 60 + secd) * 1000 + mils;
	//milsecond = (long)((min * 60 + secd) * 10000);
	milsecond = min * 60 * 1000 + secd * 1000 + mils;
	cout << "min:" << min << "   secd:" << secd << "    总的毫秒millisecond:" << milsecond << endl;
	return milsecond;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值