perlをCから呼んだり
読み込んだtsファイルを勝手にもうひとつのHDDにリネームしてコピーしてくれると嬉しいなと思い、なんか作ってみる(epgrecのデフォルトが複雑なのでちょっとだけ正規表現の練習にもなった。)。とりあえず日付とかの文字列は保存しつつ、コピーまでできるようにした。
main.c
/* * main.c * * Created on: 2011/03/11 * Author: Kuzutetsu */ #include <stdio.h> #include <libgen.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { const char *filename = basename(argv[1]); printf("ファイル名:%s\nを別HDDにコピーしたのちエンコードします。\nよろしいですか?[y/n]\n", filename); int n = 1; char c; scanf("%c",&c); if( c != 'y') { printf("終了します\n"); return 0; }else { char str[512],*ptr; FILE *fp; char command[512]; strcpy(command,"./regex_perl.pl "); strcat(command, filename); strcat(command, " "); strcat(command, argv[1]); printf("%s\n",command); if((fp=popen(command, "r"))==NULL) { fprintf(stderr,"error!\n"); exit(-1); } while(1){ fgets(str,512,fp); if(feof(fp)){ break; } ptr=strchr(str,'\n'); if(ptr!=NULL){ *ptr='\0'; } printf("%s\n",str); } pclose(fp); } return 0; }
gccのライブラリにregex.hがあるんだけど、使ってみたらひどく複雑だったのでperl先生に頼った。
#!/usr/bin/perl use utf8; use Encode; print $ARGV[0],"を分解\n"; $path = $ARGV[1]; #サンプル "2011年02月19日07時00分00秒_GR18_極上!!めちゃモテ委員長セカンドコレクション.ts"; $_ = $ARGV[0]; $yy;$mm;$dd;$hh;$mi;$sc;$tv;$name; while ( /\d{2}/g ){ push( @words, $& )} for ( 0 .. 6 ) { #print "$words[$_]\n"; $mm = $words[2]; $dd = $words[3]; $hh = $words[4]; $mi = $words[5]; $sc = $words[6]; } if(/(\d{4})/) { # print "\$1: $1\n"; $yy = $1; } if(/_([a-zA-Z0-9]+)_(\S+).ts/) { # print "\$1: $1\n"; # print "\$2: $2\n"; $tv = $1; $name = $2; } $integral = join(' ', $mm,$dd,$hh,$tv,$name); print "$integral \n"; exec "main2 $integral $path";
あとはここで保存した文字列をもとにリネームしてffmpegでエンコードすれば完璧ですね。
気が向いたら待ち時間に表示するエフェクトみたいのも作ろうか。
/* * main2.c * * Created on: 2011/03/12 * Author: Kuzutetsu */ #include <stdio.h> #include <libgen.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(int argc, char *argv[]) { pid_t result_pid; result_pid = fork(); if ( result_pid == -1 ){ //エラー printf("エラー。プログラムを終了します\n"); exit( EXIT_FAILURE ); /* 不成功終了 */ } else { if ( result_pid == 0 ){ //子プロセス printf("月:%s\n",argv[1]); printf("日:%s\n",argv[2]); printf("チャンネル:%s\n",argv[4]); printf("番組名:%s\n",argv[5]); printf("フルネーム:%s\n",argv[6]); execlp("cp","cp",argv[6],"/home3/video/temporary.ts",NULL); perror("EXEC:"); exit(1); } else { //親プロセス sleep(1); printf("%s\n",argv[5]); printf("うまくいった?\n"); } } return 0; }