胡斌

rewrite merge logic,don't use pip when there is only one video

正在显示 1 个修改的文件 包含 81 行增加80 行删除
@@ -569,60 +569,46 @@ int merge_audio_video(vector<media_info> & files) @@ -569,60 +569,46 @@ int merge_audio_video(vector<media_info> & files)
569 569
570 int merge_video_pip(vector<media_info> & files) 570 int merge_video_pip(vector<media_info> & files)
571 { 571 {
572 - char video_file[1024]; 572 + char ch0_file[1024], ch1_file[1024];
573 vector<string> merge_video_files; 573 vector<string> merge_video_files;
574 - int nsilence = 0;  
575 -  
576 - media_info video = files[0];  
577 - float start_time = video.start_time;  
578 -  
579 - for (int i = 1; i < files.size() - 1; i += 2){  
580 - media_info video = files[i];  
581 - media_info video_end = files[i + 1];  
582 - if (video.type_time - start_time > 0.1){  
583 - sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence  
584 - split_audio(silence_aac_file, 0, video.type_time - start_time, audio_file);  
585 - sprintf(video_file, "%d_%d_blank.ts", nf, nsilence++);  
586 - merge_audio_pic(audio_file, blank_pic_file, video_file);  
587 -  
588 - merge_video_files.push_back(video_file);  
589 - tmp_files.push_back(audio_file);  
590 - tmp_files.push_back(video_file);  
591 - }  
592 574
593 - if (video.type_time - video.start_time > 0.10 || video_end.end_time - video_end.type_time > 0.10) {  
594 - sprintf(video_file, "%d_%s", nf, video.name.c_str());  
595 - split_av(video.name.c_str(), video.type_time - video.start_time, video_end.type_time - video.type_time, video_file);  
596 - tmp_files.push_back(video_file);  
597 - }  
598 - else{  
599 - strcpy(video_file, video.name.c_str());  
600 - }  
601 -  
602 - start_time = video_end.type_time;  
603 - merge_video_files.push_back(video_file); 575 + media_info ch0_start, ch0_end, ch1_start, ch1_end;
  576 +
  577 + if (files[0].channel == 0) {
  578 + ch0_start = files[0];
  579 + ch0_end = files[2];
  580 + ch1_start = files[1];
  581 + ch1_end = files[3];
  582 + }
  583 + else {
  584 + ch0_start = files[1];
  585 + ch0_end = files[3];
  586 + ch1_start = files[0];
  587 + ch1_end = files[2];
  588 + }
604 589
605 - if (i == files.size() - 2){  
606 - if (video.end_time - video_end.type_time > 0.1){  
607 - sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence  
608 - split_audio(silence_aac_file, 0, video.end_time - video_end.type_time, audio_file);  
609 - sprintf(video_file, "%d_%d_blank.ts", nf, nsilence++);  
610 - merge_audio_pic(audio_file, blank_pic_file, video_file);  
611 590
612 - merge_video_files.push_back(video_file);  
613 - tmp_files.push_back(audio_file);  
614 - tmp_files.push_back(video_file);  
615 - }  
616 - } 591 + if (ch0_start.type_time - ch0_start.start_time > 0.10 || ch0_end.end_time - ch0_end.type_time > 0.10) {
  592 + sprintf(ch0_file, "%d_%s", nf, ch0_start.name.c_str());
  593 + split_av(ch0_start.name.c_str(), ch0_start.type_time - ch0_start.start_time, ch0_end.type_time - ch0_start.type_time, ch0_file);
  594 + tmp_files.push_back(ch0_file);
  595 + }
  596 + else{
  597 + strcpy(ch0_file, ch0_start.name.c_str());
617 } 598 }
618 599
619 - sprintf(video_file, "%d_merged.ts", nf);  
620 - merge_av_file(merge_video_files, video_file);  
621 - tmp_files.push_back(video_file);  
622 600
  601 + if (ch1_start.type_time - ch1_start.start_time > 0.10 || ch1_end.end_time - ch1_end.type_time > 0.10) {
  602 + sprintf(ch1_file, "%d_%s", nf, ch1_start.name.c_str());
  603 + split_av(ch1_start.name.c_str(), ch1_start.type_time - ch1_start.start_time, ch1_end.type_time - ch1_start.type_time, ch1_file);
  604 + tmp_files.push_back(ch1_file);
  605 + }
  606 + else{
  607 + strcpy(ch1_file, ch1_start.name.c_str());
  608 + }
623 609
624 sprintf(destfile, "%d.ts", nf); 610 sprintf(destfile, "%d.ts", nf);
625 - merge_video_pip(video_file, video.name.c_str(), destfile); 611 + merge_video_pip(ch0_file, ch1_file, destfile);
626 merged_files.push_back(destfile); 612 merged_files.push_back(destfile);
627 nf++; 613 nf++;
628 614
@@ -630,7 +616,7 @@ int merge_video_pip(vector<media_info> & files) @@ -630,7 +616,7 @@ int merge_video_pip(vector<media_info> & files)
630 } 616 }
631 617
632 618
633 -int transcode_files(vector<media_info> files) 619 +int transcode_file(vector<media_info> files)
634 { 620 {
635 char video_file[1024]; 621 char video_file[1024];
636 media_info video = files[0]; 622 media_info video = files[0];
@@ -727,39 +713,59 @@ int split_audio_for_video(int audio_start,vector<media_info> & result) @@ -727,39 +713,59 @@ int split_audio_for_video(int audio_start,vector<media_info> & result)
727 return 0; 713 return 0;
728 } 714 }
729 715
730 -int split_ch0_for_ch1(int ch0_start, vector<media_info> & result) 716 +int split_ch0_for_ch1(vector<media_info> & result)
731 { 717 {
732 - media_info ch0 = sorted_infos[ch0_start];  
733 - media_info ch1 = sorted_infos[ch0_start + 1]; 718 + media_info firstend = sorted_infos[2];
  719 + media_info next = sorted_infos[3];
734 720
735 result.clear(); 721 result.clear();
736 722
737 - for (int i = 0; i <= ch0_start; i++){  
738 - result.push_back(sorted_infos[i]); 723 + if (sorted_infos[0].index == firstend.index) {
  724 + for (int i = 0; i <= 2; i++){
  725 + result.push_back(sorted_infos[i]);
  726 + }
739 } 727 }
  728 + else {
  729 + result.push_back(sorted_infos[1]);
  730 + result.push_back(sorted_infos[0]);
  731 + result.push_back(sorted_infos[2]);
  732 + }
  733 +
740 734
741 - if (sorted_infos[ch0_start + 2].index == sorted_infos[ch0_start].index){  
742 - if (sorted_infos[ch0_start + 2].type_time - sorted_infos[ch0_start + 1].type_time < 0.1){//no need to split  
743 - result.push_back(sorted_infos[ch0_start + 2]);//put the audio end to the result  
744 - result.push_back(ch1);//push the video end to the result  
745 - for (int i = 0; i <= ch0_start + 2; i++){ //remove the infos including the audio end 735 +
  736 + if (next.t_type == tt_end){
  737 + if (next.type_time - firstend.type_time < 0.1){//no need to split
  738 + result.push_back(next);//push the video end to the result
  739 + for (int i = 0; i < 4; i++){ //remove the infos
746 sorted_infos.pop_front(); 740 sorted_infos.pop_front();
747 } 741 }
748 return 1; 742 return 1;
749 } 743 }
750 - }  
751 744
752 - ch0.t_type = tt_end;  
753 - ch0.type_time = ch1.type_time;  
754 - result.push_back(ch0);  
755 - result.push_back(ch1); 745 + next.type_time = firstend.type_time;
  746 + result.push_back(next);
756 747
757 - for (int i = 0; i <= ch0_start + 1; i++){ //remove the infos including the video end  
758 - sorted_infos.pop_front(); 748 + for (int i = 0; i < 3; i++){ //remove the infos including the video end
  749 + sorted_infos.pop_front();
  750 + }
  751 +
  752 + next.t_type = tt_start;
  753 + sorted_infos.push_front(next);
759 } 754 }
  755 + else {
  756 + media_info should_split = result[1];
  757 + should_split.t_type = tt_end;
  758 + should_split.type_time = firstend.type_time;
  759 + result.push_back(should_split);
760 760
761 - ch0.t_type = tt_start;  
762 - sorted_infos.push_front(ch0); 761 +
  762 + for (int i = 0; i < 3; i++){ //remove the infos including the video end
  763 + sorted_infos.pop_front();
  764 + }
  765 +
  766 + should_split.t_type = tt_start;
  767 + sorted_infos.push_front(should_split);
  768 + }
763 769
764 return 0; 770 return 0;
765 } 771 }
@@ -926,26 +932,19 @@ int process_va_files() @@ -926,26 +932,19 @@ int process_va_files()
926 while (sorted_infos.size()) 932 while (sorted_infos.size())
927 { 933 {
928 int channel = sorted_infos[0].channel; 934 int channel = sorted_infos[0].channel;
929 - if (0 == channel) {  
930 - if (!sorted_infos[1].index == sorted_infos[0].index) 935 + if (sorted_infos[1].index == sorted_infos[0].index)
931 { 936 {
932 - split_ch0_for_no_process(cur_processing);  
933 - }  
934 - else{  
935 get_front_info(1, cur_processing); 937 get_front_info(1, cur_processing);
  938 + transcode_file(cur_processing);
936 } 939 }
937 - transcode_files(cur_processing);  
938 - }  
939 - else if (1 == channel) {  
940 - int index = find_video_end();  
941 - if (is_ch0_start(index - 1)) {  
942 - split_ch0_for_ch1(index - 1, cur_processing); 940 + else if (sorted_infos[1].type_time - sorted_infos[0].type_time > 0.2) {
  941 + split_ch0_for_no_process(cur_processing);
  942 + transcode_file(cur_processing);
943 } 943 }
944 else { 944 else {
945 - get_front_info(index, cur_processing); 945 + split_ch0_for_ch1( cur_processing);
  946 + merge_video_pip(cur_processing);
946 } 947 }
947 - merge_video_pip(cur_processing);  
948 - }  
949 //if the duration between the processed end and the start of not processed is large than 200 ms, reopen a new file 948 //if the duration between the processed end and the start of not processed is large than 200 ms, reopen a new file
950 if (is_start){ 949 if (is_start){
951 start_time = cur_processing[0].start_time; 950 start_time = cur_processing[0].start_time;
@@ -968,12 +967,14 @@ int process_files() @@ -968,12 +967,14 @@ int process_files()
968 //don't split video, for a video, using merged audios to mix with it 967 //don't split video, for a video, using merged audios to mix with it
969 //for audio, mix with video or jpg 968 //for audio, mix with video or jpg
970 char outputfile[1024]; 969 char outputfile[1024];
971 - init(); 970 +
972 971
973 if (!media_files.size()){ 972 if (!media_files.size()){
974 return 0; 973 return 0;
975 } 974 }
976 975
  976 + init();
  977 +
977 fp_out_info = fopen(out_info_file, "wt"); 978 fp_out_info = fopen(out_info_file, "wt");
978 979
979 process_va_files(); 980 process_va_files();