胡斌

add some code about new merge_pip

  1 +#include "VideoDecoder.h"
  2 +
  3 +CVideoDecoder::CVideoDecoder()
  4 +{
  5 +
  6 +}
  7 +
  8 +CVideoDecoder::~CVideoDecoder()
  9 +{
  10 +}
  11 +
  12 +int CVideoDecoder::add(media_info &info)
  13 +{
  14 + return 0;
  15 +}
  16 +
  17 +unsigned int CVideoDecoder::getuid()
  18 +{
  19 + return 0;
  20 +}
  1 +#pragma once
  2 +#include <string>
  3 +#include <vector>
  4 +using namespace std;
  5 +
  6 +enum media_type{
  7 + mt_audio = 0,
  8 + mt_video = 1,
  9 + mt_av = 3,
  10 +};
  11 +
  12 +enum media_role {
  13 + mr_teacher = 0,
  14 + mr_student = 1,
  15 +};
  16 +
  17 +enum timestamp_type{
  18 + tt_start = 0,
  19 + tt_end = 1,
  20 +};
  21 +
  22 +class media_info {
  23 +public:
  24 + float type_time;//the time for start or end according to the m_type
  25 + float start_time;
  26 + float end_time;
  27 + string name;
  28 + int rotate;
  29 +
  30 + float duration;
  31 + int index;
  32 + unsigned int uid;
  33 + media_type m_type;
  34 + media_role m_role;
  35 + timestamp_type t_type;
  36 +};
  37 +
  38 +class CVideoDecoder
  39 +{
  40 +public:
  41 + CVideoDecoder();
  42 + virtual ~CVideoDecoder();
  43 +
  44 + int add(media_info &info);
  45 +
  46 + unsigned int getuid();
  47 +
  48 +protected:
  49 + vector<media_info> _info;
  50 +};
  51 +
  1 +#include "VideoTranscoder.h"
  2 +
  3 +
  4 +CVideoTranscoder::CVideoTranscoder()
  5 +{
  6 +}
  7 +
  8 +
  9 +CVideoTranscoder::~CVideoTranscoder()
  10 +{
  11 +}
  12 +
  13 +int CVideoTranscoder::add(media_info & info)
  14 +{
  15 + vector < CVideoDecoder *>::iterator it = _decoders.begin();
  16 + for (; it != _decoders.end(); it++) {
  17 + if ((*it)->getuid() == info.uid){
  18 + (*it)->add(info);
  19 + break;
  20 + }
  21 + }
  22 + if (it == _decoders.end()) {
  23 + CVideoDecoder * pVideoDecoder = new CVideoDecoder();
  24 + pVideoDecoder->add(info);
  25 + _decoders.push_back(pVideoDecoder);
  26 + }
  27 + return 0;
  28 +}
  29 +
  30 +float CVideoTranscoder::transcode()
  31 +{
  32 + throw std::logic_error("The method or operation is not implemented.");
  33 +}
  34 +
  35 +bool CVideoTranscoder::all_processed()
  36 +{
  37 + throw std::logic_error("The method or operation is not implemented.");
  38 +}
  39 +
  40 +int CVideoTranscoder::close()
  41 +{
  42 + throw std::logic_error("The method or operation is not implemented.");
  43 +}
  1 +#pragma once
  2 +#include "VideoDecoder.h"
  3 +
  4 +
  5 +class CVideoTranscoder
  6 +{
  7 +public:
  8 + CVideoTranscoder();
  9 + virtual ~CVideoTranscoder();
  10 +
  11 + int add(media_info & info);
  12 + float transcode();
  13 + bool all_processed();
  14 + int close();
  15 +
  16 +protected:
  17 + vector < CVideoDecoder *> _decoders;
  18 +};
  19 +
@@ -8,28 +8,12 @@ @@ -8,28 +8,12 @@
8 #include <list> 8 #include <list>
9 #include <deque> 9 #include <deque>
10 #include "tools.h" 10 #include "tools.h"
11 - 11 +#include "VideoTranscoder.h"
12 12
13 bool only_print = false; 13 bool only_print = false;
14 bool keep_tmp_files = false; 14 bool keep_tmp_files = false;
15 bool out_one_video = true; 15 bool out_one_video = true;
16 -using namespace std;  
17 -  
18 -enum media_type{  
19 - mt_audio = 0,  
20 - mt_video = 1,  
21 - mt_av = 3,  
22 -};  
23 16
24 -enum media_role {  
25 - mr_teacher =0,  
26 - mr_student =1,  
27 -};  
28 -  
29 -enum timestamp_type{  
30 - tt_start = 0,  
31 - tt_end = 1,  
32 -};  
33 17
34 class fileinfo { 18 class fileinfo {
35 public: 19 public:
@@ -37,25 +21,12 @@ public: @@ -37,25 +21,12 @@ public:
37 float end_time; 21 float end_time;
38 string name; 22 string name;
39 int index; 23 int index;
  24 + unsigned int uid;
40 media_type m_type; 25 media_type m_type;
41 - media_role m_rote; 26 + media_role m_role;
42 int rotate; //degree,0,90,180 ... 27 int rotate; //degree,0,90,180 ...
43 }; 28 };
44 29
45 -class media_info {  
46 -public:  
47 - float type_time;//the time for start or end according to the m_type  
48 - float start_time;  
49 - float end_time;  
50 - string name;  
51 - int rotate;  
52 -  
53 - float duration;  
54 - int index;  
55 - media_type m_type;  
56 - media_role m_rote;  
57 - timestamp_type t_type;  
58 -};  
59 30
60 31
61 vector<fileinfo> media_files; 32 vector<fileinfo> media_files;
@@ -125,7 +96,7 @@ char acodec_param[1024]; @@ -125,7 +96,7 @@ char acodec_param[1024];
125 char pip_param[1024]; 96 char pip_param[1024];
126 char pip1_param[1024]; 97 char pip1_param[1024];
127 98
128 -void addinfo(float t, string name, bool bstart,media_role role){ 99 +void addinfo(float t, string name, bool bstart,media_role role,unsigned int uid){
129 media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? mt_audio : mt_video; 100 media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? mt_audio : mt_video;
130 if (bstart) { 101 if (bstart) {
131 fileinfo f; 102 fileinfo f;
@@ -133,7 +104,8 @@ void addinfo(float t, string name, bool bstart,media_role role){ @@ -133,7 +104,8 @@ void addinfo(float t, string name, bool bstart,media_role role){
133 f.end_time = f.start_time; 104 f.end_time = f.start_time;
134 f.name = name; 105 f.name = name;
135 f.m_type = mtype; 106 f.m_type = mtype;
136 - f.m_rote = role; 107 + f.m_role = role;
  108 + f.uid = uid;
137 f.rotate = 0; 109 f.rotate = 0;
138 110
139 media_files.push_back(f); 111 media_files.push_back(f);
@@ -169,16 +141,7 @@ void addinfo(const char * t, const char * name, const char * rotation){ @@ -169,16 +141,7 @@ void addinfo(const char * t, const char * name, const char * rotation){
169 } 141 }
170 } 142 }
171 143
172 -void addinfo(float start, float duration, string name, int channel){  
173 - media_type mtype = mt_av;  
174 - fileinfo f;  
175 - f.start_time = start;  
176 - f.end_time = f.start_time + duration;  
177 - f.name = name;  
178 - f.m_type = mtype;  
179 144
180 - media_files.push_back(f);  
181 -}  
182 145
183 void split(string str, string separator, vector<string> &result, bool includeEmptyItem = false) { 146 void split(string str, string separator, vector<string> &result, bool includeEmptyItem = false) {
184 result.clear(); 147 result.clear();
@@ -501,48 +464,6 @@ void check_audio_duration() @@ -501,48 +464,6 @@ void check_audio_duration()
501 only_print = tmp; 464 only_print = tmp;
502 } 465 }
503 466
504 -int merge_audio_file(vector<string> & files, const char * dest)  
505 -{  
506 - char buf[2048],tsfile[1024];  
507 - vector<string> tsfiles;  
508 -  
509 - for (int i = 0; i < files.size(); i++){  
510 - strcpy(tsfile, files[i].c_str());  
511 - strcat(tsfile, ".ts");  
512 - tsfiles.push_back(tsfile);  
513 -  
514 - sprintf(buf, "ffmpeg -y -i %s -acodec copy -vn %s", files[i].c_str(), tsfile);  
515 - run_shell_cmd(buf);  
516 - }  
517 -  
518 - sprintf(tsfile, "m_%s.ts", dest);  
519 - concate_files(tsfiles, tsfile);  
520 -  
521 - adjust_dest_timecode(tsfile, dest);  
522 -  
523 - if (!keep_tmp_files){  
524 - tsfiles.push_back(tsfile);  
525 - removefiles(tsfiles);  
526 - }  
527 - return 0;  
528 -}  
529 -  
530 -int merge_av_file(vector<string> & files, const char * dest)  
531 -{  
532 - char tsfile[1024];  
533 - vector<string> tsfiles;  
534 -  
535 - sprintf(tsfile, "m_%s.ts", dest);  
536 - concate_files(files, tsfile);  
537 -  
538 - adjust_dest_timecode(tsfile, dest);  
539 -  
540 - if (!keep_tmp_files){  
541 - tsfiles.push_back(tsfile);  
542 - removefiles(tsfiles);  
543 - }  
544 - return 0;  
545 -}  
546 467
547 468
548 list <media_info> sorted_media; 469 list <media_info> sorted_media;
@@ -671,6 +592,8 @@ void add_media_infos() @@ -671,6 +592,8 @@ void add_media_infos()
671 m.duration = f.end_time - f.start_time; 592 m.duration = f.end_time - f.start_time;
672 m.type_time = m.start_time; 593 m.type_time = m.start_time;
673 m.rotate = f.rotate; 594 m.rotate = f.rotate;
  595 + m.m_role = f.m_role;
  596 + m.uid = f.uid;
674 add_media_info(m); 597 add_media_info(m);
675 m.t_type = tt_end; 598 m.t_type = tt_end;
676 m.type_time = m.end_time; 599 m.type_time = m.end_time;
@@ -727,477 +650,8 @@ void init_merge_pip() @@ -727,477 +650,8 @@ void init_merge_pip()
727 } 650 }
728 651
729 652
730 -int merge_audio_video(vector<media_info> & files)  
731 -{  
732 - vector<string> merge_audio_files;  
733 - int nsilence = 0;  
734 -  
735 - media_info video = files[0];  
736 - float start_time = video.start_time;  
737 -  
738 - for (int i = 1; i < files.size() - 1; i += 2){  
739 - media_info audio = files[i];  
740 - media_info audio_end = files[i + 1];  
741 - if (audio.type_time - start_time > 0.1){  
742 - sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence  
743 - split_audio(silence_aac_file, 0, audio.type_time - start_time, audio_file);  
744 - merge_audio_files.push_back(audio_file);  
745 - tmp_files.push_back(audio_file);  
746 - }  
747 -  
748 - if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio_end.type_time > 0.10) {  
749 - sprintf(audio_file, "%d_%s", nf, audio.name.c_str());  
750 - split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.type_time, audio_file);  
751 - tmp_files.push_back(audio_file);  
752 -  
753 - }  
754 - else{  
755 - strcpy(audio_file, audio.name.c_str());  
756 - }  
757 -  
758 - start_time = audio_end.type_time;  
759 - merge_audio_files.push_back(audio_file);  
760 -  
761 - if (i == files.size() - 2){  
762 - if (video.end_time - audio_end.type_time > 0.1){  
763 - sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence  
764 - split_audio(silence_aac_file, 0, video.end_time - audio_end.type_time, audio_file);  
765 - merge_audio_files.push_back(audio_file);  
766 - tmp_files.push_back(audio_file);  
767 - }  
768 - }  
769 - }  
770 -  
771 - sprintf(audio_file, "%d_merged.aac", nf);  
772 - merge_audio_file(merge_audio_files, audio_file);  
773 - tmp_files.push_back(audio_file);  
774 -  
775 -  
776 - sprintf(destfile, "%d.ts", nf);  
777 - merge_audio_video(audio_file, video.name.c_str(), video.rotate, destfile);  
778 - merged_files.push_back(destfile);  
779 - nf++;  
780 -  
781 - return 0;  
782 -}  
783 -  
784 -int merge_audio_pic(vector<media_info> & files)  
785 -{  
786 - media_info audio = files[0];  
787 - media_info audio_end = files[1];  
788 -  
789 - if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio_end.type_time > 0.10) {  
790 - sprintf(audio_file, "%d_%s", nf, audio.name.c_str());  
791 - split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.type_time, audio_file);  
792 - tmp_files.push_back(audio_file);  
793 - }  
794 - else{  
795 - strcpy(audio_file, audio.name.c_str());  
796 - }  
797 -  
798 - sprintf(destfile, "%d.ts", nf);  
799 -  
800 - int i = 0;  
801 - for (; i < sorted_infos.size(); i++){  
802 - if (sorted_infos[i].m_type == mt_video){  
803 - string name = sorted_infos[i].name;  
804 - sprintf(pic_file, "%s.jpg", name.c_str());  
805 - get_video_first_frame_jpeg(name.c_str(), pic_file);  
806 - tmp_files.push_back(pic_file);  
807 - break;  
808 - }  
809 - }  
810 - if (i == sorted_infos.size()){  
811 - strcpy(pic_file, blank_pic_file);  
812 - }  
813 -  
814 - merge_audio_pic(audio_file, pic_file, destfile);  
815 - merged_files.push_back(destfile);  
816 - nf++;  
817 -  
818 - return 0;  
819 -}  
820 -  
821 -int find_video_between_the_audio()  
822 -{  
823 - int index = sorted_infos[0].index;  
824 - int video_index = 0;  
825 - for (int i = 1; i < sorted_infos.size(); i++){  
826 - if (sorted_infos[i].index == index)  
827 - break;  
828 - if (sorted_infos[i].m_type == mt_video)  
829 - {  
830 - video_index = i;  
831 - break;  
832 - }  
833 - }  
834 - return video_index;  
835 -}  
836 -  
837 -  
838 -int transcode_file(vector<media_info> files)  
839 -{  
840 - char video_file[1024];  
841 - media_info video = files[0];  
842 - media_info video_end = files[1];  
843 -  
844 - if (video.type_time - video.start_time > 0.10 || video_end.end_time - video_end.type_time > 0.10) {  
845 - sprintf(video_file, "%d_%s", nf, video.name.c_str());  
846 - split_av_for_1pip(video.name.c_str(), video.type_time - video.start_time, video_end.type_time - video.type_time, video_file);  
847 - tmp_files.push_back(video_file);  
848 - }  
849 - else{  
850 - sprintf(video_file, "%d_%s", nf, video.name.c_str());  
851 - transcode_to_1pip(video.name.c_str(), video_file);  
852 - tmp_files.push_back(video_file);  
853 - }  
854 -  
855 - merged_files.push_back(video_file);  
856 -  
857 - nf++;  
858 -  
859 - return 0;  
860 -}  
861 -  
862 -int find_file_between_the_firstfile()  
863 -{  
864 - int index = sorted_infos[0].index;  
865 - int video_index = -1;  
866 - for (int i = 1; i < sorted_infos.size(); i++){  
867 - if (sorted_infos[i].index == index)  
868 - break;  
869 - video_index = i;  
870 -  
871 -  
872 - }  
873 - return video_index;  
874 -}  
875 -  
876 -int find_video_end()  
877 -{  
878 - int index = sorted_infos[0].index;  
879 - int video_index = 0;  
880 - for (int i = 1; i < sorted_infos.size(); i++){  
881 - if (sorted_infos[i].index == index){  
882 - video_index = i;  
883 - break;  
884 - }  
885 - }  
886 - return video_index;  
887 -}  
888 -  
889 -bool is_audio_start(int index)  
890 -{  
891 - return (sorted_infos[index].m_type == mt_audio && sorted_infos[index].t_type == tt_start);  
892 -}  
893 -  
894 -  
895 -  
896 -int split_audio_for_video(int audio_start,vector<media_info> & result)  
897 -{  
898 - media_info audio = sorted_infos[audio_start];  
899 - media_info video = sorted_infos[audio_start + 1];  
900 -  
901 - result.clear();  
902 -  
903 - for (int i = 0; i <= audio_start; i++){  
904 - result.push_back(sorted_infos[i]);  
905 - }  
906 -  
907 - if (sorted_infos[audio_start + 2].index == sorted_infos[audio_start].index){  
908 - if (sorted_infos[audio_start + 2].type_time - sorted_infos[audio_start + 1].type_time < 0.1){//no need to split  
909 - result.push_back(sorted_infos[audio_start + 2]);//put the audio end to the result  
910 - result.push_back(video);//push the video end to the result  
911 - for (int i = 0; i <= audio_start + 2; i++){ //remove the infos including the audio end  
912 - sorted_infos.pop_front();  
913 - }  
914 - return 1;  
915 - }  
916 - }  
917 -  
918 - audio.t_type = tt_end;  
919 - audio.type_time = video.type_time;  
920 - result.push_back(audio);  
921 - result.push_back(video);  
922 -  
923 - for (int i = 0; i <= audio_start +1; i++){ //remove the infos including the video end  
924 - sorted_infos.pop_front();  
925 - }  
926 -  
927 - audio.t_type = tt_start;  
928 - sorted_infos.push_front(audio);  
929 -  
930 - return 0;  
931 -}  
932 -  
933 -int split_ch0_for_ch1(vector<media_info> & result)  
934 -{  
935 - media_info firstend = sorted_infos[2];  
936 - media_info next = sorted_infos[3];  
937 -  
938 - result.clear();  
939 -  
940 - if (sorted_infos[0].index == firstend.index) {  
941 - for (int i = 0; i <= 2; i++){  
942 - result.push_back(sorted_infos[i]);  
943 - }  
944 - }  
945 - else {  
946 - result.push_back(sorted_infos[1]);  
947 - result.push_back(sorted_infos[0]);  
948 - result.push_back(sorted_infos[2]);  
949 - }  
950 -  
951 -  
952 -  
953 - if (next.t_type == tt_end){  
954 - if (next.type_time - firstend.type_time < 0.1){//no need to split  
955 - result.push_back(next);//push the video end to the result  
956 - for (int i = 0; i < 4; i++){ //remove the infos  
957 - sorted_infos.pop_front();  
958 - }  
959 - return 1;  
960 - }  
961 -  
962 - next.type_time = firstend.type_time;  
963 - result.push_back(next);  
964 -  
965 - for (int i = 0; i < 3; i++){ //remove the infos including the video end  
966 - sorted_infos.pop_front();  
967 - }  
968 -  
969 - next.t_type = tt_start;  
970 - sorted_infos.push_front(next);  
971 - }  
972 - else {  
973 - media_info should_split = result[1];  
974 - should_split.t_type = tt_end;  
975 - should_split.type_time = firstend.type_time;  
976 - result.push_back(should_split);  
977 -  
978 -  
979 - for (int i = 0; i < 3; i++){ //remove the infos including the video end  
980 - sorted_infos.pop_front();  
981 - }  
982 -  
983 - should_split.t_type = tt_start;  
984 - sorted_infos.push_front(should_split);  
985 - }  
986 -  
987 - return 0;  
988 -}  
989 -  
990 -  
991 -int split_audio_for_pic(vector<media_info> & result)  
992 -{  
993 - media_info audio = sorted_infos[0];  
994 - media_info video = sorted_infos[1];  
995 -  
996 - result.clear();  
997 -  
998 - for (int i = 0; i < 1; i++){  
999 - result.push_back(sorted_infos[i]);  
1000 - }  
1001 -  
1002 - if (sorted_infos[2].index == sorted_infos[0].index){  
1003 - if (sorted_infos[2].type_time - sorted_infos[1].type_time < 0.1){//no need to split  
1004 - result.push_back(sorted_infos[2]);//put the audio end to the result  
1005 - for (int i = 0; i < 3; i++){  
1006 - sorted_infos.pop_front();  
1007 - }  
1008 - sorted_infos.push_front(video);  
1009 - return 1;  
1010 - }  
1011 - }  
1012 -  
1013 - audio.t_type = tt_end;  
1014 - audio.type_time = video.start_time;  
1015 - result.push_back(audio);  
1016 -  
1017 - for (int i = 0; i < 2; i++){  
1018 - sorted_infos.pop_front();  
1019 - }  
1020 -  
1021 - audio.t_type = tt_start;  
1022 - sorted_infos.push_front(audio);  
1023 - sorted_infos.push_front(video);  
1024 - return 0;  
1025 -}  
1026 -  
1027 -int split_ch0_for_no_process(vector<media_info> & result)  
1028 -{  
1029 - media_info first = sorted_infos[0];  
1030 - media_info second = sorted_infos[1];  
1031 -  
1032 - result.clear();  
1033 -  
1034 - for (int i = 0; i < 1; i++){  
1035 - result.push_back(sorted_infos[i]);  
1036 - }  
1037 -  
1038 - if (sorted_infos[2].index == sorted_infos[0].index){  
1039 - if (sorted_infos[2].type_time - sorted_infos[1].type_time < 0.1){//no need to split  
1040 - result.push_back(sorted_infos[2]);//put the audio end to the result  
1041 - for (int i = 0; i < 3; i++){  
1042 - sorted_infos.pop_front();  
1043 - }  
1044 - sorted_infos.push_front(second);  
1045 - return 1;  
1046 - }  
1047 - }  
1048 -  
1049 - first.t_type = tt_end;  
1050 - first.type_time = second.start_time;  
1051 - result.push_back(first);  
1052 -  
1053 - for (int i = 0; i < 2; i++){  
1054 - sorted_infos.pop_front();  
1055 - }  
1056 653
1057 - first.t_type = tt_start;  
1058 - sorted_infos.push_front(first);  
1059 - sorted_infos.push_front(second);  
1060 - return 0;  
1061 -}  
1062 -  
1063 -void get_front_info(int index_to, vector<media_info> &cur_processing)  
1064 -{  
1065 - cur_processing.clear();  
1066 - for (int i = 0; i <= index_to; i++){  
1067 - cur_processing.push_back(sorted_infos[i]);  
1068 - }  
1069 - for (int i = 0; i <= index_to; i++){  
1070 - sorted_infos.pop_front();  
1071 - }  
1072 -}  
1073 -  
1074 -int process_va()  
1075 -{  
1076 - vector<media_info> cur_processing;  
1077 - while (sorted_infos.size())  
1078 - {  
1079 - media_type mt = sorted_infos[0].m_type;  
1080 - if (mt == mt_audio){  
1081 - int index = find_video_between_the_audio();  
1082 - if (index > 0) //have_video  
1083 - {  
1084 - split_audio_for_pic(cur_processing);  
1085 - }  
1086 - else {  
1087 - get_front_info(1, cur_processing);  
1088 - }  
1089 - merge_audio_pic(cur_processing);  
1090 - }  
1091 - else{  
1092 - int index = find_video_end();  
1093 - if (is_audio_start(index - 1)) {  
1094 - split_audio_for_video(index - 1, cur_processing);  
1095 - }  
1096 - else {  
1097 - get_front_info(index, cur_processing);  
1098 - }  
1099 - merge_audio_video(cur_processing);  
1100 - }  
1101 - }  
1102 - return 0;  
1103 -}  
1104 -  
1105 -  
1106 -int process_files_to_1file(const char * output_dest_file)  
1107 -{  
1108 - //don't split video, for a video, using merged audios to mix with it  
1109 - //for audio, mix with video or jpg  
1110 - init_merge_av();  
1111 -  
1112 - if (!media_files.size()){  
1113 - return 0;  
1114 - }  
1115 - // judge if it is only one type  
1116 - media_type mt = media_files[0].m_type;  
1117 - bool only_one_type = true;  
1118 - for (int i = 1; i < media_files.size(); i++){  
1119 - if (mt != media_files[i].m_type){  
1120 - only_one_type = false;  
1121 - break;  
1122 - }  
1123 - }  
1124 -  
1125 - if (only_one_type){  
1126 - if (mt == mt_audio) {  
1127 - if (media_files.size() == 1){  
1128 - fileinfo audio = media_files[0];  
1129 - merge_audio_pic(audio.name.c_str(), blank_pic_file, "dest.ts");  
1130 - return 0;  
1131 - }  
1132 -  
1133 - for (int i = 0; i < media_files.size(); i++){  
1134 - fileinfo audio = media_files[i];  
1135 - sprintf(destfile, "%d.ts", nf);  
1136 - merge_audio_pic(audio.name.c_str(), blank_pic_file, destfile);  
1137 - merged_files.push_back(destfile);  
1138 - nf++;  
1139 - }  
1140 - }  
1141 - else {  
1142 - if (media_files.size() == 1){  
1143 - fileinfo video = media_files[0];  
1144 - merge_video_silence(video, silence_aac_file, "dest.ts");  
1145 - return 0;  
1146 - }  
1147 -  
1148 - for (int i = 0; i < media_files.size(); i++){  
1149 - fileinfo video = media_files[i];  
1150 - sprintf(destfile, "%d.ts", nf);  
1151 - merge_video_silence(video, silence_aac_file, destfile);  
1152 - merged_files.push_back(destfile);  
1153 - nf++;  
1154 - }  
1155 - }  
1156 - }  
1157 - else {  
1158 - process_va();  
1159 - }  
1160 -  
1161 - concate_files(merged_files, "m.ts");  
1162 - tmp_files.push_back("m.ts");  
1163 -  
1164 - adjust_dest_timecode("m.ts", output_dest_file);  
1165 -  
1166 - if (!keep_tmp_files) {  
1167 - removefiles(tmp_files);  
1168 - removefiles(merged_files);  
1169 - }  
1170 -  
1171 - return 0;  
1172 -}  
1173 -  
1174 -  
1175 -int concate_files_and_adjust_timecode(const char * output_dest_file){  
1176 - if (merged_files.size() == 1){  
1177 - printf("rename %s to %s\n", merged_files[0].c_str(), output_dest_file);  
1178 - remove(output_dest_file);  
1179 - rename(merged_files[0].c_str(), output_dest_file);  
1180 - }  
1181 - else {  
1182 - concate_files(merged_files, "m.ts");  
1183 - tmp_files.push_back("m.ts");  
1184 -  
1185 - adjust_dest_timecode("m.ts", output_dest_file);  
1186 - }  
1187 -  
1188 - if (!keep_tmp_files) {  
1189 - removefiles(tmp_files);  
1190 - removefiles(merged_files);  
1191 - }  
1192 -  
1193 - merged_files.clear();  
1194 - tmp_files.clear();  
1195 -  
1196 - return 0;  
1197 -}  
1198 -  
1199 -  
1200 -int get_output_file_name(int i, const char * filename, const char * prefix,char * outputfile){ 654 +int get_output_file_name(const char * filename, const char * prefix,char * outputfile){
1201 char mainname[128]; 655 char mainname[128];
1202 const char * p = strstr(filename, "."); 656 const char * p = strstr(filename, ".");
1203 if (p) { 657 if (p) {
@@ -1208,36 +662,10 @@ int get_output_file_name(int i, const char * filename, const char * prefix,char @@ -1208,36 +662,10 @@ int get_output_file_name(int i, const char * filename, const char * prefix,char
1208 strcpy(mainname, filename); 662 strcpy(mainname, filename);
1209 } 663 }
1210 664
1211 - sprintf(outputfile, "%s%s.ts",prefix, mainname, i); 665 + sprintf(outputfile, "%s%s.ts",prefix, mainname);
1212 return 0; 666 return 0;
1213 } 667 }
1214 668
1215 -bool is_need_output(int & nOutPutFile, vector<media_info> & cur_processing, const char * first_file, char * outputfile, const char * prefix="")  
1216 -{  
1217 - if (sorted_infos.size()) {  
1218 - float lastEnd = cur_processing[cur_processing.size() - 1].type_time;  
1219 - float nextStart = sorted_infos.front().type_time;  
1220 - float gap = nextStart - lastEnd;  
1221 - if ( gap < 0.2) {  
1222 - return false;  
1223 - }  
1224 - else if(out_one_video){  
1225 - string last_merged_video = merged_files[merged_files.size() - 1];  
1226 - char buf[1024];  
1227 - sprintf(buf, "%s_last_frame.jpg", last_merged_video.c_str());  
1228 - get_video_last_frame_jpeg(last_merged_video.c_str(), buf);  
1229 - char buf_dest[1024];  
1230 - sprintf(buf_dest, "%d.ts", nOutPutFile++);  
1231 - merge_pic_silence(buf, gap, buf_dest);  
1232 - merged_files.push_back(buf_dest);  
1233 - tmp_files.push_back(buf);  
1234 - return false;  
1235 - }  
1236 - }  
1237 -  
1238 - get_output_file_name(nOutPutFile, first_file, prefix, outputfile);  
1239 - return true;  
1240 -}  
1241 669
1242 void save_out_info(float start_time, char * outputfile) 670 void save_out_info(float start_time, char * outputfile)
1243 { 671 {
@@ -1247,70 +675,27 @@ void save_out_info(float start_time, char * outputfile) @@ -1247,70 +675,27 @@ void save_out_info(float start_time, char * outputfile)
1247 } 675 }
1248 } 676 }
1249 677
1250 -int process_va_files()  
1251 -{  
1252 - char outputfile[1024];  
1253 - vector<media_info> cur_processing;  
1254 -  
1255 - int nOutPutFile = 0;  
1256 - float start_time;  
1257 - bool is_start = true;  
1258 - string start_file;  
1259 -  
1260 - while (sorted_infos.size())  
1261 - {  
1262 - media_type mt = sorted_infos[0].m_type;  
1263 - if (mt == mt_audio){  
1264 - int index = find_video_between_the_audio();  
1265 - if (index > 0) //have_video  
1266 - {  
1267 - split_audio_for_pic(cur_processing);  
1268 - }  
1269 - else {  
1270 - get_front_info(1, cur_processing);  
1271 - }  
1272 - merge_audio_pic(cur_processing);  
1273 - }  
1274 - else{  
1275 - int index = find_video_end();  
1276 - if (is_audio_start(index - 1)) {  
1277 - split_audio_for_video(index - 1, cur_processing);  
1278 - }  
1279 - else {  
1280 - get_front_info(index, cur_processing);  
1281 - }  
1282 - merge_audio_video(cur_processing);  
1283 - }  
1284 - //if the duration between the processed end and the start of not processed is large than 200 ms, reopen a new file  
1285 - if (is_start){  
1286 - start_time = cur_processing[0].start_time;  
1287 - start_file = cur_processing[0].name;  
1288 - is_start = false;  
1289 - }  
1290 - if (is_need_output(nOutPutFile, cur_processing, start_file.c_str(), outputfile, MERGED_PREFIX)){  
1291 - nOutPutFile++;  
1292 - concate_files_and_adjust_timecode(outputfile);  
1293 - save_out_info(start_time, outputfile);  
1294 - is_start = true;  
1295 - }  
1296 - }  
1297 - return 0;  
1298 -}  
1299 -  
1300 -  
1301 -  
1302 // parse the filename like 4165000_20180203013327202.aac 678 // parse the filename like 4165000_20180203013327202.aac
1303 #define get_sub_str_to_x(x , source, len, result) strncpy(x, source, len); x[len] = 0; source += len; result = atoi(x); 679 #define get_sub_str_to_x(x , source, len, result) strncpy(x, source, len); x[len] = 0; source += len; result = atoi(x);
1304 time_t time_sec_1970_base = 0; 680 time_t time_sec_1970_base = 0;
1305 -float get_start_time_from_filename(const char * filename) 681 +float get_uid_start_time_from_filename(const char * filename, unsigned int &uid)
1306 { 682 {
1307 int year, month, day, hour, min, sec, minsec; 683 int year, month, day, hour, min, sec, minsec;
1308 char buf[5]; 684 char buf[5];
1309 const char * start = strstr(filename, "_"); 685 const char * start = strstr(filename, "_");
1310 const char * end = strstr(start + 1, "_"); 686 const char * end = strstr(start + 1, "_");
1311 if (end) {//get the next 687 if (end) {//get the next
  688 + *(char *)end = 0;
  689 + uid = atoi(start + 1);
  690 + *(char *)end = '_';
  691 +
1312 start = end; 692 start = end;
1313 } 693 }
  694 + else {
  695 + *(char *)start = 0;
  696 + uid = atoi(filename);
  697 + *(char *)start = '_';
  698 + }
1314 start++; 699 start++;
1315 end = strstr(start, "."); 700 end = strstr(start, ".");
1316 701
@@ -1347,7 +732,8 @@ int readfile(const char * filename, media_role role) @@ -1347,7 +732,8 @@ int readfile(const char * filename, media_role role)
1347 return -1; 732 return -1;
1348 } 733 }
1349 734
1350 - float start_time = get_start_time_from_filename(filename); 735 + unsigned int uid = 0;
  736 + float start_time = get_uid_start_time_from_filename(filename , uid);
1351 737
1352 const int LINE_LENGTH = 1000; 738 const int LINE_LENGTH = 1000;
1353 char str[LINE_LENGTH]; 739 char str[LINE_LENGTH];
@@ -1357,10 +743,10 @@ int readfile(const char * filename, media_role role) @@ -1357,10 +743,10 @@ int readfile(const char * filename, media_role role)
1357 split(str, " ", res); 743 split(str, " ", res);
1358 if (res.size() >= 3) { 744 if (res.size() >= 3) {
1359 if (res[2] == "create"){ 745 if (res[2] == "create"){
1360 - addinfo(start_time + atof(res[0].c_str()), res[1], true, role); 746 + addinfo(start_time + atof(res[0].c_str()), res[1], true, role, uid);
1361 } 747 }
1362 else if (res[2] == "close") { 748 else if (res[2] == "close") {
1363 - addinfo(start_time + atof(res[0].c_str()), res[1], false, role); 749 + addinfo(start_time + atof(res[0].c_str()), res[1], false, role, uid);
1364 } 750 }
1365 else if (res[2] == "info") { 751 else if (res[2] == "info") {
1366 if (res.size() > 5) { 752 if (res.size() > 5) {
@@ -1483,29 +869,6 @@ int load_record_info(char * record_info) @@ -1483,29 +869,6 @@ int load_record_info(char * record_info)
1483 return 0; 869 return 0;
1484 } 870 }
1485 871
1486 -int main(int argc, char * argv[])  
1487 -{  
1488 - if (argc < 2) {  
1489 - printf(" merge_pip 2.0.0\n");  
1490 - printf(" merge video files to one pip video according to record info file,\nusage:");  
1491 - printf("\n %s record_info_filename [-p] [-k]", argv[0]);  
1492 - printf("\n -d :individual files for different time segment");  
1493 - printf("\n\n");  
1494 - return -1;  
1495 - }  
1496 -  
1497 - get_config_path();  
1498 -  
1499 - load_codec_param();  
1500 -  
1501 - for (int i = 2; i < argc; i++){  
1502 - if (!strcmp(argv[i], "-d")){  
1503 - out_one_video = false;  
1504 - }  
1505 - }  
1506 -  
1507 - load_record_info(argv[1]);  
1508 -}  
1509 872
1510 873
1511 #define __STDC_FORMAT_MACROS 874 #define __STDC_FORMAT_MACROS
@@ -1615,6 +978,10 @@ static int open_output_file(const char *filename) @@ -1615,6 +978,10 @@ static int open_output_file(const char *filename)
1615 dec_ctx = in_stream->codec; 978 dec_ctx = in_stream->codec;
1616 enc_ctx = out_stream->codec; 979 enc_ctx = out_stream->codec;
1617 980
  981 + if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  982 + enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  983 +
  984 +
1618 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO 985 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
1619 || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { 986 || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1620 /* in this example, we choose transcoding to same codec */ 987 /* in this example, we choose transcoding to same codec */
@@ -1635,6 +1002,12 @@ static int open_output_file(const char *filename) @@ -1635,6 +1002,12 @@ static int open_output_file(const char *filename)
1635 enc_ctx->pix_fmt = encoder->pix_fmts[0]; 1002 enc_ctx->pix_fmt = encoder->pix_fmts[0];
1636 /* video time_base can be set to whatever is handy and supported by encoder */ 1003 /* video time_base can be set to whatever is handy and supported by encoder */
1637 enc_ctx->time_base = dec_ctx->time_base; 1004 enc_ctx->time_base = dec_ctx->time_base;
  1005 +
  1006 + enc_ctx->me_range = 16;
  1007 + enc_ctx->max_qdiff = 4;
  1008 + enc_ctx->qmin = 10;
  1009 + enc_ctx->qmax = 30;
  1010 + enc_ctx->qcompress = 0.6;
1638 } 1011 }
1639 else { 1012 else {
1640 enc_ctx->sample_rate = dec_ctx->sample_rate; 1013 enc_ctx->sample_rate = dec_ctx->sample_rate;
@@ -1666,10 +1039,6 @@ static int open_output_file(const char *filename) @@ -1666,10 +1039,6 @@ static int open_output_file(const char *filename)
1666 return ret; 1039 return ret;
1667 } 1040 }
1668 } 1041 }
1669 -  
1670 - if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
1671 - enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;  
1672 -  
1673 } 1042 }
1674 av_dump_format(ofmt_ctx, 0, filename, 1); 1043 av_dump_format(ofmt_ctx, 0, filename, 1);
1675 1044
@@ -1972,7 +1341,7 @@ static int flush_encoder(unsigned int stream_index) @@ -1972,7 +1341,7 @@ static int flush_encoder(unsigned int stream_index)
1972 return ret; 1341 return ret;
1973 } 1342 }
1974 1343
1975 -int transcode(int argc, char *argv[]){ 1344 +int transcode(const char * input){
1976 int ret; 1345 int ret;
1977 AVPacket packet; 1346 AVPacket packet;
1978 AVFrame *frame = NULL; 1347 AVFrame *frame = NULL;
@@ -1983,17 +1352,12 @@ int transcode(int argc, char *argv[]){ @@ -1983,17 +1352,12 @@ int transcode(int argc, char *argv[]){
1983 int(*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *); 1352 int(*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
1984 memset(&packet, 0, sizeof(AVPacket)); 1353 memset(&packet, 0, sizeof(AVPacket));
1985 1354
1986 - if (argc != 3) {  
1987 - av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);  
1988 - return 1;  
1989 - } 1355 + char output[1024];
  1356 + get_output_file_name(input, "pip_", output);
1990 1357
1991 - av_register_all();  
1992 - avfilter_register_all();  
1993 -  
1994 - if ((ret = open_input_file(argv[1])) < 0) 1358 + if ((ret = open_input_file(input)) < 0)
1995 goto end; 1359 goto end;
1996 - if ((ret = open_output_file(argv[2])) < 0) 1360 + if ((ret = open_output_file(output)) < 0)
1997 goto end; 1361 goto end;
1998 if ((ret = init_filters()) < 0) 1362 if ((ret = init_filters()) < 0)
1999 goto end; 1363 goto end;
@@ -2092,4 +1456,66 @@ end: @@ -2092,4 +1456,66 @@ end:
2092 1456
2093 return ret ? 1 : 0; 1457 return ret ? 1 : 0;
2094 } 1458 }
  1459 +
  1460 +#define MIN_TIME_INTERVAL 0.2
2095 1461
  1462 +int process_av_files()
  1463 +{
  1464 + av_register_all();
  1465 + avfilter_register_all();
  1466 +
  1467 + CVideoTranscoder videoTranscoder;
  1468 +
  1469 + float cur_time = 0.0;
  1470 + bool has_file = sorted_media.size();
  1471 + while (has_file){
  1472 + while (has_file){
  1473 + media_info info = sorted_media.front();
  1474 +
  1475 + if (info.start_time - cur_time < MIN_TIME_INTERVAL) {
  1476 + sorted_media.pop_front();
  1477 + videoTranscoder.add(info);
  1478 + }
  1479 + else {
  1480 + break;
  1481 + }
  1482 + has_file = sorted_media.size();
  1483 + }
  1484 +
  1485 + cur_time = videoTranscoder.transcode();
  1486 + }
  1487 +
  1488 + while (videoTranscoder.all_processed()){
  1489 + cur_time = videoTranscoder.transcode();
  1490 + }
  1491 +
  1492 + videoTranscoder.close();
  1493 +
  1494 + return 0;
  1495 +}
  1496 +
  1497 +int main(int argc, char * argv[])
  1498 +{
  1499 + if (argc < 2) {
  1500 + printf(" merge_pip 2.0.0\n");
  1501 + printf(" merge video files to one pip video according to record info file,\nusage:");
  1502 + printf("\n %s record_info_filename [-p] [-k]", argv[0]);
  1503 + printf("\n -d :individual files for different time segment");
  1504 + printf("\n\n");
  1505 + return -1;
  1506 + }
  1507 +
  1508 + get_config_path();
  1509 +
  1510 + load_codec_param();
  1511 +
  1512 + for (int i = 2; i < argc; i++){
  1513 + if (!strcmp(argv[i], "-d")){
  1514 + out_one_video = false;
  1515 + }
  1516 + }
  1517 +
  1518 + load_record_info(argv[1]);
  1519 +
  1520 + process_av_files();
  1521 +}
@@ -84,6 +84,12 @@ @@ -84,6 +84,12 @@
84 <ItemGroup> 84 <ItemGroup>
85 <ClCompile Include="merge_pip.cpp" /> 85 <ClCompile Include="merge_pip.cpp" />
86 <ClCompile Include="tools.cpp" /> 86 <ClCompile Include="tools.cpp" />
  87 + <ClCompile Include="VideoDecoder.cpp" />
  88 + <ClCompile Include="VideoTranscoder.cpp" />
  89 + </ItemGroup>
  90 + <ItemGroup>
  91 + <ClInclude Include="VideoDecoder.h" />
  92 + <ClInclude Include="VideoTranscoder.h" />
87 </ItemGroup> 93 </ItemGroup>
88 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> 94 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
89 <ImportGroup Label="ExtensionTargets"> 95 <ImportGroup Label="ExtensionTargets">
@@ -24,5 +24,19 @@ @@ -24,5 +24,19 @@
24 <ClCompile Include="merge_pip.cpp"> 24 <ClCompile Include="merge_pip.cpp">
25 <Filter>源文件</Filter> 25 <Filter>源文件</Filter>
26 </ClCompile> 26 </ClCompile>
  27 + <ClCompile Include="VideoTranscoder.cpp">
  28 + <Filter>源文件</Filter>
  29 + </ClCompile>
  30 + <ClCompile Include="VideoDecoder.cpp">
  31 + <Filter>源文件</Filter>
  32 + </ClCompile>
  33 + </ItemGroup>
  34 + <ItemGroup>
  35 + <ClInclude Include="VideoTranscoder.h">
  36 + <Filter>头文件</Filter>
  37 + </ClInclude>
  38 + <ClInclude Include="VideoDecoder.h">
  39 + <Filter>头文件</Filter>
  40 + </ClInclude>
27 </ItemGroup> 41 </ItemGroup>
28 </Project> 42 </Project>