본문 바로가기
C 언어

C언어로 자막 파일 수정하기

by treeCoder 2021. 1. 2.

동영상의 자막파일이 있는데, 너무 한 화면에 나오는 자막의 길이가 길 때 다음 프로그램을 사용하여 수정이 가능하다.

자세한 사용법은 추후에 올리도록 할 예정이고, 우선 Code만 소개한다.

 

다음의 코드를 fix_youtube_srt.c에 저장한다.

 

#include <stdio.h>  // 
#include <stdlib.h> // malloc
#include <string.h>

typedef struct Node {
    char word[128];
    struct Node *next;
} Node;

Node *push(Node *head, char *myword) {

     Node *stone=(Node *) malloc(sizeof(Node));
     strcpy(stone->word, myword);

     stone->next=head;
     head = stone;

     return head;
}

int print(Node *head) { Node *p;

    strcpy(head->word, "1"); /* just because you have strange char on the first data */

    for(p = head; p != NULL; p = p->next) {
        printf("%s\n", p->word);
    }

    return 0;
}

Node *clear(Node *head) { Node *p, *tmp;

   for(p=head;p!=NULL;p=tmp) {
       tmp=p->next;
       free(p);
   }

   return (head = NULL);
}

Node *reverse(Node *head) { Node *p, *head2=NULL;
   
   Node *tmp, *stone;
   
   for(p=head; p!=NULL; p=tmp) {
       tmp  =p->next;
       stone=p;
       
       stone->next=head2;
       head2=stone;
   }
   
   return head2;
}

Node *go_back_4(Node* head) {
    
    Node* p = head;  /* Initialize current  */
    int cnt1 = 0;

    while (p != NULL)  { 
        p = p->next; 
        if(++cnt1 == 4 ) return p;
    } 
    return NULL; 


int main( ) { Node *head = NULL; Node *p; Node *t; char *ptr;

    char buf[256];
    char cpy[256];

    while( fgets(buf, sizeof(buf)-1, stdin) != NULL ) {
           buf[strcspn(buf, "\n")]=0; /* remove new line char */
           head = push(head, buf);
    }

    for(p = head; p != NULL; p = p->next) {
        strcpy(cpy, p->word);

        if((ptr=strstr(cpy, " -->"))) {
            *ptr=0; /* get 1st part before --> */

            t = go_back_4(p); /* go back 4 nodes */
            if( t ==  NULL ) break;
            
            if( (ptr=strstr(t->word, " --> ")) ) { 
                ptr += 5;  *ptr = 0;   /* cut 2nd part */
                strcat(t->word, cpy);
            }
        }     
    }

    head = reverse(head);
    print(head);

    return 0;
}

댓글