胡斌

part of merge the code of merge_av and merge_pip,not finished

正在显示 1 个修改的文件 包含 291 行增加8 行删除
@@ -382,6 +382,25 @@ void get_duration_from_video_file() @@ -382,6 +382,25 @@ void get_duration_from_video_file()
382 } 382 }
383 383
384 384
  385 +void check_audio_duration()
  386 +{
  387 + bool tmp = only_print;
  388 + only_print = false;
  389 +
  390 + for (int i = 0; i < media_files.size(); i++){
  391 + if (media_files[i].m_type == mt_audio) {
  392 + fileinfo f = media_files[i];
  393 + if (f.end_time - f.start_time < 0.1){//avoid no close
  394 + float duration = get_file_duration(f.name.c_str(), false);
  395 + media_files[i].end_time = f.start_time + duration;
  396 + printf("file:%s , duration in recording file maybe is not set, duration parsed from file: %.3f\n", f.name.c_str(), duration);
  397 + }
  398 + }
  399 + }
  400 +
  401 + only_print = tmp;
  402 +}
  403 +
385 int merge_audio_file(vector<string> & files, const char * dest) 404 int merge_audio_file(vector<string> & files, const char * dest)
386 { 405 {
387 char buf[2048],tsfile[1024]; 406 char buf[2048],tsfile[1024];
@@ -474,7 +493,7 @@ void add_media_infos() @@ -474,7 +493,7 @@ void add_media_infos()
474 } 493 }
475 494
476 list<media_info>::iterator it = sorted_media.begin(); 495 list<media_info>::iterator it = sorted_media.begin();
477 - for (int i=0; it != sorted_media.end(); it++, i++ ){ 496 + for (; it != sorted_media.end(); it++){
478 sorted_infos.push_back(*it); 497 sorted_infos.push_back(*it);
479 } 498 }
480 } 499 }
@@ -496,14 +515,25 @@ void unifiy_start_time() @@ -496,14 +515,25 @@ void unifiy_start_time()
496 } 515 }
497 } 516 }
498 517
499 -void init() 518 +void init_merge_av()
500 { 519 {
  520 +
501 strcpy(blank_pic_file, cfg_path); 521 strcpy(blank_pic_file, cfg_path);
502 strcat(blank_pic_file, "blank.jpg"); 522 strcat(blank_pic_file, "blank.jpg");
503 523
504 strcpy(silence_aac_file, cfg_path); 524 strcpy(silence_aac_file, cfg_path);
505 strcat(silence_aac_file, "silence.aac"); 525 strcat(silence_aac_file, "silence.aac");
506 526
  527 + check_audio_duration();
  528 + get_duration_from_video_file();
  529 + add_media_infos();
  530 +
  531 + nv = 0;
  532 + nf = 0;
  533 +}
  534 +
  535 +void init_merge_pip()
  536 +{
507 unifiy_start_time(); 537 unifiy_start_time();
508 538
509 add_media_infos(); 539 add_media_infos();
@@ -567,6 +597,59 @@ int merge_audio_video(vector<media_info> & files) @@ -567,6 +597,59 @@ int merge_audio_video(vector<media_info> & files)
567 return 0; 597 return 0;
568 } 598 }
569 599
  600 +int merge_audio_pic(vector<media_info> & files)
  601 +{
  602 + media_info audio = files[0];
  603 + media_info audio_end = files[1];
  604 +
  605 + if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio_end.type_time > 0.10) {
  606 + sprintf(audio_file, "%d_%s", nf, audio.name.c_str());
  607 + split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.type_time, audio_file);
  608 + tmp_files.push_back(audio_file);
  609 + }
  610 + else{
  611 + strcpy(audio_file, audio.name.c_str());
  612 + }
  613 +
  614 + sprintf(destfile, "%d.ts", nf);
  615 +
  616 + int i = 0;
  617 + for (; i < sorted_infos.size(); i++){
  618 + if (sorted_infos[i].m_type == mt_video){
  619 + string name = sorted_infos[i].name;
  620 + sprintf(pic_file, "%s.jpg", name.c_str());
  621 + get_video_first_frame_jpeg(name.c_str(), pic_file);
  622 + tmp_files.push_back(pic_file);
  623 + break;
  624 + }
  625 + }
  626 + if (i == sorted_infos.size()){
  627 + strcpy(pic_file, blank_pic_file);
  628 + }
  629 +
  630 + merge_audio_pic(audio_file, pic_file, destfile);
  631 + merged_files.push_back(destfile);
  632 + nf++;
  633 +
  634 + return 0;
  635 +}
  636 +
  637 +int find_video_between_the_audio()
  638 +{
  639 + int index = sorted_infos[0].index;
  640 + int video_index = 0;
  641 + for (int i = 1; i < sorted_infos.size(); i++){
  642 + if (sorted_infos[i].index == index)
  643 + break;
  644 + if (sorted_infos[i].m_type == mt_video)
  645 + {
  646 + video_index = i;
  647 + break;
  648 + }
  649 + }
  650 + return video_index;
  651 +}
  652 +
570 int merge_video_pip(vector<media_info> & files) 653 int merge_video_pip(vector<media_info> & files)
571 { 654 {
572 char ch0_file[1024], ch1_file[1024]; 655 char ch0_file[1024], ch1_file[1024];
@@ -854,6 +937,105 @@ void get_front_info(int index_to, vector<media_info> &cur_processing) @@ -854,6 +937,105 @@ void get_front_info(int index_to, vector<media_info> &cur_processing)
854 } 937 }
855 } 938 }
856 939
  940 +int process_va()
  941 +{
  942 + vector<media_info> cur_processing;
  943 + while (sorted_infos.size())
  944 + {
  945 + media_type mt = sorted_infos[0].m_type;
  946 + if (mt == mt_audio){
  947 + int index = find_video_between_the_audio();
  948 + if (index > 0) //have_video
  949 + {
  950 + split_audio_for_pic(cur_processing);
  951 + }
  952 + else {
  953 + get_front_info(1, cur_processing);
  954 + }
  955 + merge_audio_pic(cur_processing);
  956 + }
  957 + else{
  958 + int index = find_video_end();
  959 + if (is_audio_start(index - 1)) {
  960 + split_audio_for_video(index - 1, cur_processing);
  961 + }
  962 + else {
  963 + get_front_info(index, cur_processing);
  964 + }
  965 + merge_audio_video(cur_processing);
  966 + }
  967 + }
  968 + return 0;
  969 +}
  970 +
  971 +
  972 +int process_files_to_1file(const char * output_dest_file)
  973 +{
  974 + //don't split video, for a video, using merged audios to mix with it
  975 + //for audio, mix with video or jpg
  976 + init_merge_av();
  977 +
  978 + if (!media_files.size()){
  979 + return 0;
  980 + }
  981 + // judge if it is only one type
  982 + media_type mt = media_files[0].m_type;
  983 + bool only_one_type = true;
  984 + for (int i = 1; i < media_files.size(); i++){
  985 + if (mt != media_files[i].m_type){
  986 + only_one_type = false;
  987 + break;
  988 + }
  989 + }
  990 +
  991 + if (only_one_type){
  992 + if (mt == mt_audio) {
  993 + if (media_files.size() == 1){
  994 + fileinfo audio = media_files[0];
  995 + merge_audio_pic(audio.name.c_str(), blank_pic_file, "dest.ts");
  996 + return 0;
  997 + }
  998 +
  999 + for (int i = 0; i < media_files.size(); i++){
  1000 + fileinfo audio = media_files[i];
  1001 + sprintf(destfile, "%d.ts", nf);
  1002 + merge_audio_pic(audio.name.c_str(), blank_pic_file, destfile);
  1003 + merged_files.push_back(destfile);
  1004 + nf++;
  1005 + }
  1006 + }
  1007 + else {
  1008 + if (media_files.size() == 1){
  1009 + fileinfo video = media_files[0];
  1010 + merge_video_silence(video, silence_aac_file, "dest.ts");
  1011 + return 0;
  1012 + }
  1013 +
  1014 + for (int i = 0; i < media_files.size(); i++){
  1015 + fileinfo video = media_files[i];
  1016 + sprintf(destfile, "%d.ts", nf);
  1017 + merge_video_silence(video, silence_aac_file, destfile);
  1018 + merged_files.push_back(destfile);
  1019 + nf++;
  1020 + }
  1021 + }
  1022 + }
  1023 + else {
  1024 + process_va();
  1025 + }
  1026 +
  1027 + concate_files(merged_files, "m.ts");
  1028 + tmp_files.push_back("m.ts");
  1029 +
  1030 + adjust_dest_timecode("m.ts", output_dest_file);
  1031 +
  1032 + if (!keep_tmp_files) {
  1033 + removefiles(tmp_files);
  1034 + removefiles(merged_files);
  1035 + }
  1036 +
  1037 + return 0;
  1038 +}
857 1039
858 1040
859 int concate_files_and_adjust_timecode(const char * output_dest_file){ 1041 int concate_files_and_adjust_timecode(const char * output_dest_file){
@@ -918,7 +1100,6 @@ void save_out_info(float start_time, char * outputfile) @@ -918,7 +1100,6 @@ void save_out_info(float start_time, char * outputfile)
918 } 1100 }
919 } 1101 }
920 1102
921 -  
922 int process_va_files() 1103 int process_va_files()
923 { 1104 {
924 char outputfile[1024]; 1105 char outputfile[1024];
@@ -931,6 +1112,56 @@ int process_va_files() @@ -931,6 +1112,56 @@ int process_va_files()
931 1112
932 while (sorted_infos.size()) 1113 while (sorted_infos.size())
933 { 1114 {
  1115 + media_type mt = sorted_infos[0].m_type;
  1116 + if (mt == mt_audio){
  1117 + int index = find_video_between_the_audio();
  1118 + if (index > 0) //have_video
  1119 + {
  1120 + split_audio_for_pic(cur_processing);
  1121 + }
  1122 + else {
  1123 + get_front_info(1, cur_processing);
  1124 + }
  1125 + merge_audio_pic(cur_processing);
  1126 + }
  1127 + else{
  1128 + int index = find_video_end();
  1129 + if (is_audio_start(index - 1)) {
  1130 + split_audio_for_video(index - 1, cur_processing);
  1131 + }
  1132 + else {
  1133 + get_front_info(index, cur_processing);
  1134 + }
  1135 + merge_audio_video(cur_processing);
  1136 + }
  1137 + //if the duration between the processed end and the start of not processed is large than 200 ms, reopen a new file
  1138 + if (is_start){
  1139 + start_time = cur_processing[0].start_time;
  1140 + start_file = cur_processing[0].name;
  1141 + is_start = false;
  1142 + }
  1143 + if (is_need_output(nOutPutFile, cur_processing, start_file.c_str(), outputfile)){
  1144 + nOutPutFile++;
  1145 + concate_files_and_adjust_timecode(outputfile);
  1146 + save_out_info(start_time, outputfile);
  1147 + is_start = true;
  1148 + }
  1149 + }
  1150 + return 0;
  1151 +}
  1152 +
  1153 +int process_merged_files()
  1154 +{
  1155 + char outputfile[1024];
  1156 + vector<media_info> cur_processing;
  1157 +
  1158 + int nOutPutFile = 0;
  1159 + float start_time;
  1160 + bool is_start = true;
  1161 + string start_file;
  1162 +
  1163 + while (sorted_infos.size())
  1164 + {
934 int channel = sorted_infos[0].channel; 1165 int channel = sorted_infos[0].channel;
935 if (sorted_infos[1].index == sorted_infos[0].index) 1166 if (sorted_infos[1].index == sorted_infos[0].index)
936 { 1167 {
@@ -961,8 +1192,59 @@ int process_va_files() @@ -961,8 +1192,59 @@ int process_va_files()
961 return 0; 1192 return 0;
962 } 1193 }
963 1194
  1195 +int process_record_file_to_ts()
  1196 +{
  1197 + //don't split video, for a video, using merged audios to mix with it
  1198 + //for audio, mix with video or jpg
  1199 + char outputfile[1024];
  1200 + init_merge_av();
  1201 +
  1202 + if (!media_files.size()){
  1203 + return 0;
  1204 + }
  1205 +
  1206 + fp_out_info = fopen(out_info_file, "wt");
  1207 +
  1208 + // judge if it is only one type
  1209 + media_type mt = media_files[0].m_type;
  1210 + bool only_one_type = true;
  1211 + for (int i = 1; i < media_files.size(); i++){
  1212 + if (mt != media_files[i].m_type){
  1213 + only_one_type = false;
  1214 + break;
  1215 + }
  1216 + }
964 1217
965 -int process_files() 1218 + if (only_one_type){
  1219 + if (mt == mt_audio) {
  1220 + for (int i = 0; i < media_files.size(); i++){
  1221 + fileinfo audio = media_files[i];
  1222 + get_output_file_name(i, audio.name.c_str(), outputfile);
  1223 + merge_audio_pic(audio.name.c_str(), blank_pic_file, outputfile);
  1224 + save_out_info(audio.start_time, outputfile);
  1225 + }
  1226 + }
  1227 + else {
  1228 + for (int i = 0; i < media_files.size(); i++){
  1229 + fileinfo video = media_files[i];
  1230 + get_output_file_name(i, video.name.c_str(), outputfile);
  1231 + merge_video_silence(video, silence_aac_file, destfile);
  1232 + save_out_info(video.start_time, outputfile);
  1233 + }
  1234 + }
  1235 + }
  1236 + else {
  1237 + process_va_files();
  1238 + }
  1239 +
  1240 + if (fp_out_info) {
  1241 + fclose(fp_out_info);
  1242 + }
  1243 +
  1244 + return 0;
  1245 +}
  1246 +
  1247 +int process_merged_files_to_pip_files()
966 { 1248 {
967 //don't split video, for a video, using merged audios to mix with it 1249 //don't split video, for a video, using merged audios to mix with it
968 //for audio, mix with video or jpg 1250 //for audio, mix with video or jpg
@@ -973,11 +1255,11 @@ int process_files() @@ -973,11 +1255,11 @@ int process_files()
973 return 0; 1255 return 0;
974 } 1256 }
975 1257
976 - init(); 1258 + init_merge_pip();
977 1259
978 fp_out_info = fopen(out_info_file, "wt"); 1260 fp_out_info = fopen(out_info_file, "wt");
979 1261
980 - process_va_files(); 1262 + process_merged_files();
981 1263
982 if (fp_out_info) { 1264 if (fp_out_info) {
983 fclose(fp_out_info); 1265 fclose(fp_out_info);
@@ -1010,6 +1292,7 @@ int readfile(char * filename) @@ -1010,6 +1292,7 @@ int readfile(char * filename)
1010 } 1292 }
1011 return 0; 1293 return 0;
1012 } 1294 }
  1295 +
1013 // parse the filename like 4165000_20180203013327202.aac 1296 // parse the filename like 4165000_20180203013327202.aac
1014 #define get_sub_str_to_x(x , source, len, result) strncpy(x, source, len); x[len] = 0; source += len; result = atoi(x); 1297 #define get_sub_str_to_x(x , source, len, result) strncpy(x, source, len); x[len] = 0; source += len; result = atoi(x);
1015 time_t time_sec_1970_base = 0; 1298 time_t time_sec_1970_base = 0;
@@ -1083,7 +1366,7 @@ void load_codec_param() @@ -1083,7 +1366,7 @@ void load_codec_param()
1083 char cfgfile[1024]; 1366 char cfgfile[1024];
1084 1367
1085 strcpy(cfgfile, cfg_path); 1368 strcpy(cfgfile, cfg_path);
1086 - strcat(cfgfile, "merge_av.cfg"); 1369 + strcat(cfgfile, "merge_pip.cfg");
1087 1370
1088 ifstream fin(cfgfile); 1371 ifstream fin(cfgfile);
1089 if (!fin) { 1372 if (!fin) {
@@ -1164,7 +1447,7 @@ int main(int argc, char * argv[]) @@ -1164,7 +1447,7 @@ int main(int argc, char * argv[])
1164 1447
1165 load_codec_param(); 1448 load_codec_param();
1166 1449
1167 - process_files(); 1450 + process_merged_files_to_pip_files();
1168 1451
1169 return 0; 1452 return 0;
1170 } 1453 }