正在显示
1 个修改的文件
包含
88 行增加
和
33 行删除
| @@ -52,6 +52,9 @@ public: | @@ -52,6 +52,9 @@ public: | ||
| 52 | 52 | ||
| 53 | vector<fileinfo> media_files; | 53 | vector<fileinfo> media_files; |
| 54 | 54 | ||
| 55 | +const char * MERGED_PREFIX = ""; | ||
| 56 | +const char * PIP_PREFIX = "pip_"; | ||
| 57 | + | ||
| 55 | 58 | ||
| 56 | void run_shell_cmd(const char * cmd) | 59 | void run_shell_cmd(const char * cmd) |
| 57 | { | 60 | { |
| @@ -113,6 +116,12 @@ char pip_param[1024]; | @@ -113,6 +116,12 @@ char pip_param[1024]; | ||
| 113 | bool first_time_set = false; | 116 | bool first_time_set = false; |
| 114 | float start_time = 0.0f; | 117 | float start_time = 0.0f; |
| 115 | 118 | ||
| 119 | +void init_read_file() { | ||
| 120 | + first_time_set = false; | ||
| 121 | + start_time = 0.0f; | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | + | ||
| 116 | void addinfo(string t, string name, bool bstart){ | 125 | void addinfo(string t, string name, bool bstart){ |
| 117 | media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? mt_audio : mt_video; | 126 | media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? mt_audio : mt_video; |
| 118 | if (bstart) { | 127 | if (bstart) { |
| @@ -450,7 +459,6 @@ deque<media_info> sorted_infos; | @@ -450,7 +459,6 @@ deque<media_info> sorted_infos; | ||
| 450 | vector<media_info> media_infos; | 459 | vector<media_info> media_infos; |
| 451 | vector<string > merged_files; | 460 | vector<string > merged_files; |
| 452 | vector<string> tmp_files; | 461 | vector<string> tmp_files; |
| 453 | -int nv; // the index of processing video file | ||
| 454 | int nf;//the index of processing target merged ts | 462 | int nf;//the index of processing target merged ts |
| 455 | char destfile[1024], audio_file[1024], pic_file[1024]; | 463 | char destfile[1024], audio_file[1024], pic_file[1024]; |
| 456 | char blank_pic_file[1024]; | 464 | char blank_pic_file[1024]; |
| @@ -474,6 +482,7 @@ void add_media_info(media_info m) | @@ -474,6 +482,7 @@ void add_media_info(media_info m) | ||
| 474 | 482 | ||
| 475 | void add_media_infos() | 483 | void add_media_infos() |
| 476 | { | 484 | { |
| 485 | + sorted_media.clear(); | ||
| 477 | for (int i = 0; i < media_files.size(); i++) { | 486 | for (int i = 0; i < media_files.size(); i++) { |
| 478 | fileinfo f = media_files[i]; | 487 | fileinfo f = media_files[i]; |
| 479 | media_info m; | 488 | media_info m; |
| @@ -517,7 +526,6 @@ void unifiy_start_time() | @@ -517,7 +526,6 @@ void unifiy_start_time() | ||
| 517 | 526 | ||
| 518 | void init_merge_av() | 527 | void init_merge_av() |
| 519 | { | 528 | { |
| 520 | - | ||
| 521 | strcpy(blank_pic_file, cfg_path); | 529 | strcpy(blank_pic_file, cfg_path); |
| 522 | strcat(blank_pic_file, "blank.jpg"); | 530 | strcat(blank_pic_file, "blank.jpg"); |
| 523 | 531 | ||
| @@ -528,8 +536,8 @@ void init_merge_av() | @@ -528,8 +536,8 @@ void init_merge_av() | ||
| 528 | get_duration_from_video_file(); | 536 | get_duration_from_video_file(); |
| 529 | add_media_infos(); | 537 | add_media_infos(); |
| 530 | 538 | ||
| 531 | - nv = 0; | ||
| 532 | - nf = 0; | 539 | + merged_files.clear(); |
| 540 | + tmp_files.clear(); | ||
| 533 | } | 541 | } |
| 534 | 542 | ||
| 535 | void init_merge_pip() | 543 | void init_merge_pip() |
| @@ -538,8 +546,8 @@ void init_merge_pip() | @@ -538,8 +546,8 @@ void init_merge_pip() | ||
| 538 | 546 | ||
| 539 | add_media_infos(); | 547 | add_media_infos(); |
| 540 | 548 | ||
| 541 | - nv = 0; | ||
| 542 | - nf = 0; | 549 | + merged_files.clear(); |
| 550 | + tmp_files.clear(); | ||
| 543 | } | 551 | } |
| 544 | 552 | ||
| 545 | 553 | ||
| @@ -1063,22 +1071,22 @@ int concate_files_and_adjust_timecode(const char * output_dest_file){ | @@ -1063,22 +1071,22 @@ int concate_files_and_adjust_timecode(const char * output_dest_file){ | ||
| 1063 | } | 1071 | } |
| 1064 | 1072 | ||
| 1065 | 1073 | ||
| 1066 | -int get_output_file_name(int i, const char * file_prefix, char * outputfile){ | ||
| 1067 | - char prefix[128]; | ||
| 1068 | - const char * p = strstr(file_prefix, "."); | 1074 | +int get_output_file_name(int i, const char * filename, const char * prefix,char * outputfile){ |
| 1075 | + char mainname[128]; | ||
| 1076 | + const char * p = strstr(filename, "."); | ||
| 1069 | if (p) { | 1077 | if (p) { |
| 1070 | - strncpy(prefix, file_prefix, p - file_prefix); | ||
| 1071 | - prefix[p - file_prefix] = 0; | 1078 | + strncpy(mainname, filename, p - filename); |
| 1079 | + mainname[p - filename] = 0; | ||
| 1072 | } | 1080 | } |
| 1073 | else { | 1081 | else { |
| 1074 | - strcpy(prefix, file_prefix); | 1082 | + strcpy(mainname, filename); |
| 1075 | } | 1083 | } |
| 1076 | 1084 | ||
| 1077 | - sprintf(outputfile, "pip_%s.ts", prefix, i); | 1085 | + sprintf(outputfile, "%s%s.ts",prefix, mainname, i); |
| 1078 | return 0; | 1086 | return 0; |
| 1079 | } | 1087 | } |
| 1080 | 1088 | ||
| 1081 | -bool is_need_output(int nOutPutFile, vector<media_info> & cur_processing, const char * first_file, char * outputfile) | 1089 | +bool is_need_output(int nOutPutFile, vector<media_info> & cur_processing, const char * first_file, char * outputfile, const char * prefix="") |
| 1082 | { | 1090 | { |
| 1083 | if (sorted_infos.size()) { | 1091 | if (sorted_infos.size()) { |
| 1084 | int lastEnd = cur_processing[cur_processing.size() - 1].type_time; | 1092 | int lastEnd = cur_processing[cur_processing.size() - 1].type_time; |
| @@ -1088,7 +1096,7 @@ bool is_need_output(int nOutPutFile, vector<media_info> & cur_processing, const | @@ -1088,7 +1096,7 @@ bool is_need_output(int nOutPutFile, vector<media_info> & cur_processing, const | ||
| 1088 | } | 1096 | } |
| 1089 | } | 1097 | } |
| 1090 | 1098 | ||
| 1091 | - get_output_file_name(nOutPutFile, first_file, outputfile); | 1099 | + get_output_file_name(nOutPutFile, first_file, prefix, outputfile); |
| 1092 | return true; | 1100 | return true; |
| 1093 | } | 1101 | } |
| 1094 | 1102 | ||
| @@ -1140,7 +1148,7 @@ int process_va_files() | @@ -1140,7 +1148,7 @@ int process_va_files() | ||
| 1140 | start_file = cur_processing[0].name; | 1148 | start_file = cur_processing[0].name; |
| 1141 | is_start = false; | 1149 | is_start = false; |
| 1142 | } | 1150 | } |
| 1143 | - if (is_need_output(nOutPutFile, cur_processing, start_file.c_str(), outputfile)){ | 1151 | + if (is_need_output(nOutPutFile, cur_processing, start_file.c_str(), outputfile, MERGED_PREFIX)){ |
| 1144 | nOutPutFile++; | 1152 | nOutPutFile++; |
| 1145 | concate_files_and_adjust_timecode(outputfile); | 1153 | concate_files_and_adjust_timecode(outputfile); |
| 1146 | save_out_info(start_time, outputfile); | 1154 | save_out_info(start_time, outputfile); |
| @@ -1182,7 +1190,7 @@ int process_merged_files() | @@ -1182,7 +1190,7 @@ int process_merged_files() | ||
| 1182 | start_file = cur_processing[0].name; | 1190 | start_file = cur_processing[0].name; |
| 1183 | is_start = false; | 1191 | is_start = false; |
| 1184 | } | 1192 | } |
| 1185 | - if (is_need_output(nOutPutFile, cur_processing, start_file.c_str(), outputfile)){ | 1193 | + if (is_need_output(nOutPutFile, cur_processing, start_file.c_str(), outputfile, PIP_PREFIX)){ |
| 1186 | nOutPutFile++; | 1194 | nOutPutFile++; |
| 1187 | concate_files_and_adjust_timecode(outputfile); | 1195 | concate_files_and_adjust_timecode(outputfile); |
| 1188 | save_out_info(start_time, outputfile); | 1196 | save_out_info(start_time, outputfile); |
| @@ -1219,7 +1227,7 @@ int process_record_file_to_ts() | @@ -1219,7 +1227,7 @@ int process_record_file_to_ts() | ||
| 1219 | if (mt == mt_audio) { | 1227 | if (mt == mt_audio) { |
| 1220 | for (int i = 0; i < media_files.size(); i++){ | 1228 | for (int i = 0; i < media_files.size(); i++){ |
| 1221 | fileinfo audio = media_files[i]; | 1229 | fileinfo audio = media_files[i]; |
| 1222 | - get_output_file_name(i, audio.name.c_str(), outputfile); | 1230 | + get_output_file_name(i, audio.name.c_str(), MERGED_PREFIX, outputfile); |
| 1223 | merge_audio_pic(audio.name.c_str(), blank_pic_file, outputfile); | 1231 | merge_audio_pic(audio.name.c_str(), blank_pic_file, outputfile); |
| 1224 | save_out_info(audio.start_time, outputfile); | 1232 | save_out_info(audio.start_time, outputfile); |
| 1225 | } | 1233 | } |
| @@ -1227,7 +1235,7 @@ int process_record_file_to_ts() | @@ -1227,7 +1235,7 @@ int process_record_file_to_ts() | ||
| 1227 | else { | 1235 | else { |
| 1228 | for (int i = 0; i < media_files.size(); i++){ | 1236 | for (int i = 0; i < media_files.size(); i++){ |
| 1229 | fileinfo video = media_files[i]; | 1237 | fileinfo video = media_files[i]; |
| 1230 | - get_output_file_name(i, video.name.c_str(), outputfile); | 1238 | + get_output_file_name(i, video.name.c_str(), MERGED_PREFIX, outputfile); |
| 1231 | merge_video_silence(video, silence_aac_file, destfile); | 1239 | merge_video_silence(video, silence_aac_file, destfile); |
| 1232 | save_out_info(video.start_time, outputfile); | 1240 | save_out_info(video.start_time, outputfile); |
| 1233 | } | 1241 | } |
| @@ -1270,6 +1278,9 @@ int process_merged_files_to_pip_files() | @@ -1270,6 +1278,9 @@ int process_merged_files_to_pip_files() | ||
| 1270 | 1278 | ||
| 1271 | int readfile(char * filename) | 1279 | int readfile(char * filename) |
| 1272 | { | 1280 | { |
| 1281 | + init_read_file(); | ||
| 1282 | + media_files.clear(); | ||
| 1283 | + | ||
| 1273 | ifstream fin(filename); | 1284 | ifstream fin(filename); |
| 1274 | if (!fin) { | 1285 | if (!fin) { |
| 1275 | return -1; | 1286 | return -1; |
| @@ -1329,8 +1340,12 @@ float get_start_time_from_filename(const char * filename) | @@ -1329,8 +1340,12 @@ float get_start_time_from_filename(const char * filename) | ||
| 1329 | return (float)(t) + minsec / 1000.0; | 1340 | return (float)(t) + minsec / 1000.0; |
| 1330 | } | 1341 | } |
| 1331 | 1342 | ||
| 1343 | +vector<string> all_input_files_for_pip; | ||
| 1344 | + | ||
| 1332 | int readfile(char * filename, int channel) | 1345 | int readfile(char * filename, int channel) |
| 1333 | { | 1346 | { |
| 1347 | + init_read_file(); | ||
| 1348 | + | ||
| 1334 | ifstream fin(filename); | 1349 | ifstream fin(filename); |
| 1335 | if (!fin) { | 1350 | if (!fin) { |
| 1336 | return -1; | 1351 | return -1; |
| @@ -1346,10 +1361,11 @@ int readfile(char * filename, int channel) | @@ -1346,10 +1361,11 @@ int readfile(char * filename, int channel) | ||
| 1346 | split(str, " ", res); | 1361 | split(str, " ", res); |
| 1347 | if (res.size() == 3) { | 1362 | if (res.size() == 3) { |
| 1348 | if (bstart_time) { | 1363 | if (bstart_time) { |
| 1349 | - start_time = get_start_time_from_filename(res[2].c_str()); | 1364 | + start_time = get_start_time_from_filename(res[2].c_str() + strlen(MERGED_PREFIX)); |
| 1350 | bstart_time = false; | 1365 | bstart_time = false; |
| 1351 | } | 1366 | } |
| 1352 | addinfo(atof(res[0].c_str()) + start_time ,atof(res[1].c_str()) , res[2], channel); | 1367 | addinfo(atof(res[0].c_str()) + start_time ,atof(res[1].c_str()) , res[2], channel); |
| 1368 | + all_input_files_for_pip.push_back(res[2]); | ||
| 1353 | } | 1369 | } |
| 1354 | } | 1370 | } |
| 1355 | return 0; | 1371 | return 0; |
| @@ -1411,26 +1427,30 @@ void get_outinfo_file_name(char * input1 , char * input2) | @@ -1411,26 +1427,30 @@ void get_outinfo_file_name(char * input1 , char * input2) | ||
| 1411 | strcat(out_info_file, input2); | 1427 | strcat(out_info_file, input2); |
| 1412 | } | 1428 | } |
| 1413 | 1429 | ||
| 1430 | +void get_outinfo_file_name(char * input) | ||
| 1431 | +{ | ||
| 1432 | + strcpy(out_info_file, input); | ||
| 1433 | + char * p = strstr(out_info_file, "."); | ||
| 1434 | + if (p) { | ||
| 1435 | + *p = 0; | ||
| 1436 | + } | ||
| 1437 | + strcat(out_info_file, "_out.txt"); | ||
| 1438 | +} | ||
| 1439 | + | ||
| 1414 | int main(int argc, char * argv[]) | 1440 | int main(int argc, char * argv[]) |
| 1415 | { | 1441 | { |
| 1416 | if (argc < 3) { | 1442 | if (argc < 3) { |
| 1417 | printf(" pip 1.0.0\n"); | 1443 | printf(" pip 1.0.0\n"); |
| 1418 | - printf(" run ffmpeg to merge video files to one pip video according to the processed record info file,\nusage:"); | ||
| 1419 | - printf("\n %s record_info_out_filename1 record_info_out_filename2 [-p] [-k]", argv[0]); | 1444 | + printf(" run ffmpeg to merge video files to one pip video according to record info file,\nusage:"); |
| 1445 | + printf("\n %s record_info_filename1 record_filename2 [-p] [-k]", argv[0]); | ||
| 1420 | printf("\n -p :only print the command,don't run ffmpeg"); | 1446 | printf("\n -p :only print the command,don't run ffmpeg"); |
| 1421 | printf("\n -k :keep the temp files\n"); | 1447 | printf("\n -k :keep the temp files\n"); |
| 1422 | return -1; | 1448 | return -1; |
| 1423 | } | 1449 | } |
| 1424 | 1450 | ||
| 1425 | - if (readfile(argv[1], 0) < 0) { | ||
| 1426 | - printf("open file: %s error", argv[1]); | ||
| 1427 | - return -2; | ||
| 1428 | - } | 1451 | + get_config_path(); |
| 1429 | 1452 | ||
| 1430 | - if (readfile(argv[2], 1) < 0) { | ||
| 1431 | - printf("open file: %s error", argv[1]); | ||
| 1432 | - return -2; | ||
| 1433 | - } | 1453 | + load_codec_param(); |
| 1434 | 1454 | ||
| 1435 | for (int i = 3; i < argc; i++){ | 1455 | for (int i = 3; i < argc; i++){ |
| 1436 | if (!strcmp(argv[i], "-p")){ | 1456 | if (!strcmp(argv[i], "-p")){ |
| @@ -1441,14 +1461,49 @@ int main(int argc, char * argv[]) | @@ -1441,14 +1461,49 @@ int main(int argc, char * argv[]) | ||
| 1441 | } | 1461 | } |
| 1442 | } | 1462 | } |
| 1443 | 1463 | ||
| 1444 | - get_outinfo_file_name(argv[1], argv[2]); | 1464 | + char merged_info1[1024]; |
| 1465 | + if (readfile(argv[1]) < 0) { | ||
| 1466 | + printf("open file: %s error", argv[1]); | ||
| 1467 | + return -2; | ||
| 1468 | + } | ||
| 1445 | 1469 | ||
| 1446 | - get_config_path(); | 1470 | + get_outinfo_file_name(argv[1]); |
| 1471 | + strcpy(merged_info1, out_info_file); | ||
| 1472 | + process_record_file_to_ts(); | ||
| 1447 | 1473 | ||
| 1448 | - load_codec_param(); | 1474 | + |
| 1475 | + char merged_info2[1024]; | ||
| 1476 | + if (readfile(argv[2]) < 0) { | ||
| 1477 | + printf("open file: %s error", argv[1]); | ||
| 1478 | + return -2; | ||
| 1479 | + } | ||
| 1480 | + | ||
| 1481 | + get_outinfo_file_name(argv[2]); | ||
| 1482 | + strcpy(merged_info2, out_info_file); | ||
| 1483 | + process_record_file_to_ts(); | ||
| 1484 | + | ||
| 1485 | + | ||
| 1486 | + media_files.clear(); | ||
| 1487 | + if (readfile(merged_info1, 0) < 0) { | ||
| 1488 | + printf("open file: %s error", merged_info1); | ||
| 1489 | + return -2; | ||
| 1490 | + } | ||
| 1491 | + | ||
| 1492 | + if (readfile(merged_info2, 1) < 0) { | ||
| 1493 | + printf("open file: %s error", merged_info2); | ||
| 1494 | + return -2; | ||
| 1495 | + } | ||
| 1496 | + | ||
| 1497 | + get_outinfo_file_name(argv[1], argv[2]); | ||
| 1449 | 1498 | ||
| 1450 | process_merged_files_to_pip_files(); | 1499 | process_merged_files_to_pip_files(); |
| 1451 | 1500 | ||
| 1501 | + if (!keep_tmp_files) { | ||
| 1502 | + removefiles(all_input_files_for_pip); | ||
| 1503 | + remove_file(merged_info1); | ||
| 1504 | + remove_file(merged_info2); | ||
| 1505 | + } | ||
| 1506 | + | ||
| 1452 | return 0; | 1507 | return 0; |
| 1453 | } | 1508 | } |
| 1454 | 1509 |
-
请 注册 或 登录 后发表评论