⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 str2lrc.c

📁 将字幕转成歌词
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
//#include <stdbool.h>
#define LEN 40
#define CNUM 36
int isnumber(char);
int isstr (char []);
int isenter (char,char);
//void putcc (char,char,FILE *);
int main (int argc,char *argv[])
{
	FILE *in,*out;  //declare two FILE pointers
	int i,j;
	char ch,temp1,temp2;
	char name[LEN]; //storage for output filename
	char temp[CNUM];
	//char temp1;
	int x = CNUM;
	
	//check for the command-line arguments 
	if(argc < 2)
	{
		fprintf(stderr,"Usage: %s filename\n",argv[0]);
		exit(1);
	}
	
	//set up input
	if((in = fopen(argv[1],"r")) == NULL)
	{
		fprintf(stderr,"Couldn't open the file \"%s\"!\n",argv[1]);
		exit(2);
	}
	
	//set up output
	strncpy(name,argv[1],strlen(argv[1])-4); //copy filename
	name[strlen(argv[1])-4] = '\0';
	strcat(name,".lrc"); //append .lrc
	if((out = fopen(name,"w")) == NULL)
	{
		fprintf(stderr,"Can't creat output file.\n");
		exit(3);
	}
	
	//copy data
	while((ch = getc(in)) != EOF)
	{
		if(ch == '\n')
		{
			if((temp1 = getc(in)) == '\n')
			{
				putc('\n',out);
				if(isdigit(temp2 = getc(in))) //判断是不是连续两个回车 然后紧接着的的是一个数字
				{
					putc(temp1,out);
					temp[0] = temp2;
					for(i = 1;i < CNUM;i++)
					{
						ch = getc(in);
						temp[i] = ch;
						//printf("%c",temp[i]);
						//if(ch == EOF)
						//	break;
					}
					
					/*********当包含"-->"时 处理字幕时间为歌词时间*******/ 
					if ((i = isstr(temp)))
					{
						for(j = 0;j < i-15;j++)
							putc(temp[j],out);
						for(j = i-15;j < i-1;j++)
						{
							putc(temp[j],out);
							if(temp[j] == '\n')
								putc('[',out);
						}
						putc(']',out);
					
					
					/*****处理剩下的字符*****/
						for(j = i+16;j < CNUM;j++)
						{
							if(temp[j] == '\n')
								x = j;
								
							if(j > x) //这边编的不太好 最好用一个函数 
								{
									if(temp[j] == '\n')
									{	
										if((temp[j+1]) == '\n')
										{
											putc(temp[j],out);
										}
												
										else
										{
											putc(' ',out); // print 
										}
									}
									else
									{
										putc(temp[j],out);
									}
								}
						} 
					}
					else
					{
						for(i = 0;i <CNUM;i++)
						{
							if(temp[i] == '\n')
							{	
								if((temp[i+1]) == '\n')
								{
								putc(temp[i],out);
								}
											
								else
								{
								putc(' ',out); // print 
								}
							}
							else
							{
								putc(temp[i],out);
							}		
						}
					}
				}
				else
				{
					putc(' ',out);
			  		putc(temp2,out);
				}
				
				
			}
			else
			{
				putc(' ',out);
				putc(temp1,out);
			}
				
		}
		else
		putc(ch,out);
	}
		
	
	//clean up
	if((fclose(in) != 0) || (fclose(out) != 0))
		fprintf(stderr,"Error in closing files.\n");
	return 0;
	}

/*int isnumber (char ch)
{
	if((ch >= '0')&&(ch <= '9'))
	return 1;
	else
	return 0;
}*/
int isstr (char temp[]) //if is str time
{
	int flag = 0;
	int i;
	for(i = 0;i < CNUM;i++)
	{
		if((temp[i] == '-')&&(temp[i+1] == '-')&&(temp[i+2] == '>'))
		{
			if(((temp[i-8]) == ':')||((temp[i-7]) == ':'))
			{
				flag = i;
				break;
			}
			else
				flag = 0;
		}
		else
			flag = 0;
			
	}
	return flag;
}

/*void putcc(char ch,char temp1,FILE *out)
{
	
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -