胡斌

seems ok

@@ -169,31 +169,18 @@ void get_video_first_frame_jpeg(const char * video, const char * destfile) @@ -169,31 +169,18 @@ void get_video_first_frame_jpeg(const char * video, const char * destfile)
169 run_shell_cmd(buf); 169 run_shell_cmd(buf);
170 } 170 }
171 171
172 -void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfile)  
173 -{  
174 - char buf[2048];  
175 - sprintf(buf, "ffmpeg -y -loop 1 -i %s.jpg -i %d_%s -loop 0 -shortest %s %s %s", video.name.c_str(), nf, audio.name.c_str(), acodec_param, vcodec_param, destfile);  
176 - run_shell_cmd(buf);  
177 -}  
178 -  
179 -void merge_audio_pic(fileinfo audio, int nf, const char * picfile, const char * destfile) 172 +void merge_audio_pic(const char * audio, const char * picfile, const char * destfile)
180 { 173 {
181 char buf[2048]; 174 char buf[2048];
182 - sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %d_%s -loop 0 -shortest %s %s %s", picfile, nf, audio.name.c_str(), acodec_param, vcodec_param, destfile); 175 + sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio, acodec_param, vcodec_param, destfile);
183 run_shell_cmd(buf); 176 run_shell_cmd(buf);
184 } 177 }
185 178
186 -void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile)  
187 -{  
188 - char buf[2048];  
189 - sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio.name.c_str(), acodec_param, vcodec_param, destfile);  
190 - run_shell_cmd(buf);  
191 -}  
192 179
193 -void merge_audio_pic(const char * audio, const char * picfile, const char * destfile) 180 +void merge_audio_video(const char * audio, const char * video, const char * destfile)
194 { 181 {
195 char buf[2048]; 182 char buf[2048];
196 - sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio, acodec_param, vcodec_param, destfile); 183 + sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile);
197 run_shell_cmd(buf); 184 run_shell_cmd(buf);
198 } 185 }
199 186
@@ -204,19 +191,6 @@ void merge_video_silence(fileinfo video, const char * aacfile, const char * dest @@ -204,19 +191,6 @@ void merge_video_silence(fileinfo video, const char * aacfile, const char * dest
204 run_shell_cmd(buf); 191 run_shell_cmd(buf);
205 } 192 }
206 193
207 -void merge_audio_video(fileinfo audio, int nf, fileinfo video, const char * destfile)  
208 -{  
209 - char buf[2048];  
210 - sprintf(buf, "ffmpeg -y -i %d_%s -i %s %s %s %s", nf, audio.name.c_str(), video.name.c_str(), acodec_param, vcodec_param, destfile);  
211 - run_shell_cmd(buf);  
212 -}  
213 -  
214 -void merge_audio_video(const char * audio, const char * video, const char * destfile)  
215 -{  
216 - char buf[2048];  
217 - sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile);  
218 - run_shell_cmd(buf);  
219 -}  
220 194
221 void concate_files(vector<string > merged_files, const char * destfile) 195 void concate_files(vector<string > merged_files, const char * destfile)
222 { 196 {
@@ -418,46 +392,21 @@ int merge_audio_file(vector<string> & files, const char * dest) @@ -418,46 +392,21 @@ int merge_audio_file(vector<string> & files, const char * dest)
418 return 0; 392 return 0;
419 } 393 }
420 394
421 -class MergeProcess{  
422 -public:  
423 - MergeProcess();  
424 - int process_files(const char * output_dest_file);  
425 -  
426 -protected:  
427 - void add_media_info(media_info m);  
428 - void add_media_infos();  
429 - void init();  
430 - int merge_audio_video(vector<media_info> & files);  
431 - int merge_audio_pic(vector<media_info> & files);  
432 - int find_video_between_the_audio();  
433 - int find_video_end();  
434 - bool is_audio_start(int index);  
435 - int split_audio(int index_from, vector<media_info> & result);  
436 - void get_front_info(int index_to, vector<media_info> &cur_processing);  
437 - int process_va();  
438 -  
439 -protected:  
440 - list <media_info> sorted_media;  
441 - deque<media_info> sorted_infos;  
442 - vector<media_info> media_infos;  
443 - vector<string > merged_files;  
444 - vector<string> tmp_files;  
445 - int nv; // the index of processing video file  
446 - int nf;//the index of processing target merged ts  
447 - char destfile[1024], audio_file[1024], pic_file[1024];  
448 - char blank_pic_file[1024];  
449 - char silence_aac_file[1024];  
450 - int audio_index;  
451 - int audio_start;  
452 - fileinfo audio;  
453 - fileinfo video;  
454 -};  
455 395
456 -MergeProcess::MergeProcess(){  
457 - init();  
458 -} 396 +list <media_info> sorted_media;
  397 +deque<media_info> sorted_infos;
  398 +vector<media_info> media_infos;
  399 +vector<string > merged_files;
  400 +vector<string> tmp_files;
  401 +int nv; // the index of processing video file
  402 +int nf;//the index of processing target merged ts
  403 +char destfile[1024], audio_file[1024], pic_file[1024];
  404 +char blank_pic_file[1024];
  405 +char silence_aac_file[1024];
  406 +
459 407
460 -void MergeProcess::add_media_info(media_info m) 408 +
  409 +void add_media_info(media_info m)
461 { 410 {
462 list<media_info>::iterator it = sorted_media.begin(); 411 list<media_info>::iterator it = sorted_media.begin();
463 for (; it != sorted_media.end(); it++){ 412 for (; it != sorted_media.end(); it++){
@@ -468,7 +417,7 @@ void MergeProcess::add_media_info(media_info m) @@ -468,7 +417,7 @@ void MergeProcess::add_media_info(media_info m)
468 sorted_media.insert(it, m); 417 sorted_media.insert(it, m);
469 } 418 }
470 419
471 -void MergeProcess::add_media_infos() 420 +void add_media_infos()
472 { 421 {
473 for (int i = 0; i < media_files.size(); i++) { 422 for (int i = 0; i < media_files.size(); i++) {
474 fileinfo f = media_files[i]; 423 fileinfo f = media_files[i];
@@ -493,7 +442,7 @@ void MergeProcess::add_media_infos() @@ -493,7 +442,7 @@ void MergeProcess::add_media_infos()
493 } 442 }
494 } 443 }
495 444
496 -void MergeProcess::init() 445 +void init()
497 { 446 {
498 strcpy(blank_pic_file, cfg_path); 447 strcpy(blank_pic_file, cfg_path);
499 strcat(blank_pic_file, "blank.jpg"); 448 strcat(blank_pic_file, "blank.jpg");
@@ -507,13 +456,12 @@ void MergeProcess::init() @@ -507,13 +456,12 @@ void MergeProcess::init()
507 456
508 nv = 0; 457 nv = 0;
509 nf = 0; 458 nf = 0;
510 - audio_index = 0;  
511 } 459 }
512 460
513 461
514 -int MergeProcess::merge_audio_video(vector<media_info> & files) 462 +int merge_audio_video(vector<media_info> & files)
515 { 463 {
516 - vector<std::string > merge_audio_files; 464 + vector<string> merge_audio_files;
517 int nsilence = 0; 465 int nsilence = 0;
518 466
519 media_info video = files[0]; 467 media_info video = files[0];
@@ -524,14 +472,14 @@ int MergeProcess::merge_audio_video(vector<media_info> & files) @@ -524,14 +472,14 @@ int MergeProcess::merge_audio_video(vector<media_info> & files)
524 media_info audio_end = files[i + 1]; 472 media_info audio_end = files[i + 1];
525 if (audio.type_time - start_time > 0.1){ 473 if (audio.type_time - start_time > 0.1){
526 sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence 474 sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence
527 - ::split_audio(silence_aac_file, 0, audio.type_time - start_time, audio_file); 475 + split_audio(silence_aac_file, 0, audio.type_time - start_time, audio_file);
528 merge_audio_files.push_back(silence_aac_file); 476 merge_audio_files.push_back(silence_aac_file);
529 tmp_files.push_back(silence_aac_file); 477 tmp_files.push_back(silence_aac_file);
530 } 478 }
531 479
532 if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio.type_time > 0.10) { 480 if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio.type_time > 0.10) {
533 sprintf(audio_file, "%d_%s", nf, audio.name.c_str()); 481 sprintf(audio_file, "%d_%s", nf, audio.name.c_str());
534 - ::split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file); 482 + split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file);
535 tmp_files.push_back(audio_file); 483 tmp_files.push_back(audio_file);
536 484
537 } 485 }
@@ -545,7 +493,7 @@ int MergeProcess::merge_audio_video(vector<media_info> & files) @@ -545,7 +493,7 @@ int MergeProcess::merge_audio_video(vector<media_info> & files)
545 if (i == files.size() - 2){ 493 if (i == files.size() - 2){
546 if (video.end_time - audio_end.type_time > 0.1){ 494 if (video.end_time - audio_end.type_time > 0.1){
547 sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence 495 sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence
548 - ::split_audio(silence_aac_file, 0, video.end_time - audio_end.type_time, audio_file); 496 + split_audio(silence_aac_file, 0, video.end_time - audio_end.type_time, audio_file);
549 merge_audio_files.push_back(silence_aac_file); 497 merge_audio_files.push_back(silence_aac_file);
550 tmp_files.push_back(silence_aac_file); 498 tmp_files.push_back(silence_aac_file);
551 } 499 }
@@ -557,21 +505,21 @@ int MergeProcess::merge_audio_video(vector<media_info> & files) @@ -557,21 +505,21 @@ int MergeProcess::merge_audio_video(vector<media_info> & files)
557 505
558 506
559 sprintf(destfile, "%d.ts", nf); 507 sprintf(destfile, "%d.ts", nf);
560 - ::merge_audio_video(audio_file, video.name.c_str(), destfile); 508 + merge_audio_video(audio_file, video.name.c_str(), destfile);
561 merged_files.push_back(destfile); 509 merged_files.push_back(destfile);
562 nf++; 510 nf++;
563 511
564 return 0; 512 return 0;
565 } 513 }
566 514
567 -int MergeProcess::merge_audio_pic(vector<media_info> & files) 515 +int merge_audio_pic(vector<media_info> & files)
568 { 516 {
569 media_info audio = files[0]; 517 media_info audio = files[0];
570 media_info audio_end = files[1]; 518 media_info audio_end = files[1];
571 519
572 if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio.type_time > 0.10) { 520 if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio.type_time > 0.10) {
573 sprintf(audio_file, "%d_%s", nf, audio.name.c_str()); 521 sprintf(audio_file, "%d_%s", nf, audio.name.c_str());
574 - ::split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file); 522 + split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file);
575 tmp_files.push_back(audio_file); 523 tmp_files.push_back(audio_file);
576 } 524 }
577 else{ 525 else{
@@ -594,14 +542,14 @@ int MergeProcess::merge_audio_pic(vector<media_info> & files) @@ -594,14 +542,14 @@ int MergeProcess::merge_audio_pic(vector<media_info> & files)
594 strcpy(pic_file, blank_pic_file); 542 strcpy(pic_file, blank_pic_file);
595 } 543 }
596 544
597 - ::merge_audio_pic(audio_file, pic_file, destfile); 545 + merge_audio_pic(audio_file, pic_file, destfile);
598 merged_files.push_back(destfile); 546 merged_files.push_back(destfile);
599 nf++; 547 nf++;
600 548
601 return 0; 549 return 0;
602 } 550 }
603 551
604 -int MergeProcess::find_video_between_the_audio() 552 +int find_video_between_the_audio()
605 { 553 {
606 int index = sorted_infos[0].index; 554 int index = sorted_infos[0].index;
607 int video_index = 0; 555 int video_index = 0;
@@ -617,7 +565,7 @@ int MergeProcess::find_video_between_the_audio() @@ -617,7 +565,7 @@ int MergeProcess::find_video_between_the_audio()
617 return video_index; 565 return video_index;
618 } 566 }
619 567
620 -int MergeProcess::find_video_end() 568 +int find_video_end()
621 { 569 {
622 int index = sorted_infos[0].index; 570 int index = sorted_infos[0].index;
623 int video_index = 0; 571 int video_index = 0;
@@ -630,30 +578,68 @@ int MergeProcess::find_video_end() @@ -630,30 +578,68 @@ int MergeProcess::find_video_end()
630 return video_index; 578 return video_index;
631 } 579 }
632 580
633 -bool MergeProcess::is_audio_start(int index) 581 +bool is_audio_start(int index)
634 { 582 {
635 return (sorted_infos[index].m_type == mt_audio && sorted_infos[index].t_type == tt_start); 583 return (sorted_infos[index].m_type == mt_audio && sorted_infos[index].t_type == tt_start);
636 } 584 }
637 585
638 586
639 -int MergeProcess::split_audio(int index_from, vector<media_info> & result) 587 +int split_audio_for_video(int audio_start,vector<media_info> & result)
  588 +{
  589 + media_info audio = sorted_infos[audio_start];
  590 + media_info video = sorted_infos[audio_start + 1];
  591 +
  592 + result.clear();
  593 +
  594 + for (int i = 0; i <= audio_start; i++){
  595 + result.push_back(sorted_infos[i]);
  596 + }
  597 +
  598 + if (sorted_infos[audio_start + 2].index == sorted_infos[audio_start].index){
  599 + if (sorted_infos[audio_start + 2].type_time - sorted_infos[audio_start + 1].type_time < 0.1){//no need to split
  600 + result.push_back(sorted_infos[audio_start + 2]);//put the audio end to the result
  601 + result.push_back(video);//push the video end to the result
  602 + for (int i = 0; i <= audio_start + 2; i++){ //remove the infos including the audio end
  603 + sorted_infos.pop_front();
  604 + }
  605 + return 0;
  606 + }
  607 + }
  608 +
  609 + audio.t_type = tt_end;
  610 + audio.type_time = video.type_time;
  611 + result.push_back(audio);
  612 + result.push_back(video);
  613 +
  614 + for (int i = 0; i <= audio_start +1; i++){ //remove the infos including the video end
  615 + sorted_infos.pop_front();
  616 + }
  617 +
  618 + audio.t_type = tt_start;
  619 + sorted_infos.push_front(audio);
  620 +
  621 + return 0;
  622 +}
  623 +
  624 +
  625 +int split_audio_for_pic(vector<media_info> & result)
640 { 626 {
641 - media_info audio = sorted_infos[index_from];  
642 - media_info video = sorted_infos[index_from + 1]; 627 + media_info audio = sorted_infos[0];
  628 + media_info video = sorted_infos[1];
643 629
644 result.clear(); 630 result.clear();
645 631
646 - for (int i = 0; i < index_from; i++){ 632 + for (int i = 0; i < 1; i++){
647 result.push_back(sorted_infos[i]); 633 result.push_back(sorted_infos[i]);
648 } 634 }
649 635
650 - if (sorted_infos[index_from + 2].index == sorted_infos[index_from].index){  
651 - if (sorted_infos[index_from].end_time - sorted_infos[index_from + 1].start_time < 0.1){//no need to split  
652 - result.push_back(sorted_infos[index_from + 2]);//put the audio end to the result  
653 - for (int i = 0; i < index_from + 2; i++){ 636 + if (sorted_infos[2].index == sorted_infos[0].index){
  637 + if (sorted_infos[2].type_time - sorted_infos[1].type_time < 0.1){//no need to split
  638 + result.push_back(sorted_infos[2]);//put the audio end to the result
  639 + for (int i = 0; i < 3; i++){
654 sorted_infos.pop_front(); 640 sorted_infos.pop_front();
655 } 641 }
656 - sorted_infos.push_front(video); 642 + sorted_infos.push_front(video);
657 return 0; 643 return 0;
658 } 644 }
659 } 645 }
@@ -662,7 +648,7 @@ int MergeProcess::split_audio(int index_from, vector<media_info> & result) @@ -662,7 +648,7 @@ int MergeProcess::split_audio(int index_from, vector<media_info> & result)
662 audio.type_time = video.start_time; 648 audio.type_time = video.start_time;
663 result.push_back(audio); 649 result.push_back(audio);
664 650
665 - for (int i = 0; i < index_from + 1; i++){ 651 + for (int i = 0; i < 2; i++){
666 sorted_infos.pop_front(); 652 sorted_infos.pop_front();
667 } 653 }
668 654
@@ -672,7 +658,7 @@ int MergeProcess::split_audio(int index_from, vector<media_info> & result) @@ -672,7 +658,7 @@ int MergeProcess::split_audio(int index_from, vector<media_info> & result)
672 return 0; 658 return 0;
673 } 659 }
674 660
675 -void MergeProcess::get_front_info(int index_to, vector<media_info> &cur_processing) 661 +void get_front_info(int index_to, vector<media_info> &cur_processing)
676 { 662 {
677 cur_processing.clear(); 663 cur_processing.clear();
678 for (int i = 0; i <= index_to; i++){ 664 for (int i = 0; i <= index_to; i++){
@@ -683,7 +669,7 @@ void MergeProcess::get_front_info(int index_to, vector<media_info> &cur_processi @@ -683,7 +669,7 @@ void MergeProcess::get_front_info(int index_to, vector<media_info> &cur_processi
683 } 669 }
684 } 670 }
685 671
686 -int MergeProcess::process_va() 672 +int process_va()
687 { 673 {
688 vector<media_info> cur_processing; 674 vector<media_info> cur_processing;
689 while (sorted_infos.size()) 675 while (sorted_infos.size())
@@ -693,7 +679,7 @@ int MergeProcess::process_va() @@ -693,7 +679,7 @@ int MergeProcess::process_va()
693 int index = find_video_between_the_audio(); 679 int index = find_video_between_the_audio();
694 if (index > 0) //have_video 680 if (index > 0) //have_video
695 { 681 {
696 - split_audio(index, cur_processing); 682 + split_audio_for_pic(cur_processing);
697 } 683 }
698 else { 684 else {
699 get_front_info(1, cur_processing); 685 get_front_info(1, cur_processing);
@@ -703,7 +689,7 @@ int MergeProcess::process_va() @@ -703,7 +689,7 @@ int MergeProcess::process_va()
703 else{ 689 else{
704 int index = find_video_end(); 690 int index = find_video_end();
705 if (is_audio_start(index - 1)) { 691 if (is_audio_start(index - 1)) {
706 - split_audio(index - 1, cur_processing); 692 + split_audio_for_video(index - 1, cur_processing);
707 } 693 }
708 else { 694 else {
709 get_front_info(index, cur_processing); 695 get_front_info(index, cur_processing);
@@ -715,14 +701,15 @@ int MergeProcess::process_va() @@ -715,14 +701,15 @@ int MergeProcess::process_va()
715 } 701 }
716 702
717 703
718 -int MergeProcess::process_files(const char * output_dest_file) 704 +int process_files(const char * output_dest_file)
719 { 705 {
720 //don't split video, for a video, using merged audios to mix with it 706 //don't split video, for a video, using merged audios to mix with it
721 //for audio, mix with video or jpg 707 //for audio, mix with video or jpg
  708 + init();
  709 +
722 if (!media_files.size()){ 710 if (!media_files.size()){
723 return 0; 711 return 0;
724 } 712 }
725 -  
726 // judge if it is only one type 713 // judge if it is only one type
727 media_type mt = media_files[0].m_type; 714 media_type mt = media_files[0].m_type;
728 bool only_one_type = true; 715 bool only_one_type = true;
@@ -737,14 +724,14 @@ int MergeProcess::process_files(const char * output_dest_file) @@ -737,14 +724,14 @@ int MergeProcess::process_files(const char * output_dest_file)
737 if (mt == mt_audio) { 724 if (mt == mt_audio) {
738 if (media_files.size() == 1){ 725 if (media_files.size() == 1){
739 fileinfo audio = media_files[0]; 726 fileinfo audio = media_files[0];
740 - ::merge_audio_pic(audio, blank_pic_file, "dest.ts"); 727 + merge_audio_pic(audio.name.c_str(), blank_pic_file, "dest.ts");
741 return 0; 728 return 0;
742 } 729 }
743 730
744 for (int i = 0; i < media_files.size(); i++){ 731 for (int i = 0; i < media_files.size(); i++){
745 fileinfo audio = media_files[i]; 732 fileinfo audio = media_files[i];
746 sprintf(destfile, "%d.ts", nf); 733 sprintf(destfile, "%d.ts", nf);
747 - ::merge_audio_pic(audio, blank_pic_file, destfile); 734 + merge_audio_pic(audio.name.c_str(), blank_pic_file, destfile);
748 merged_files.push_back(destfile); 735 merged_files.push_back(destfile);
749 nf++; 736 nf++;
750 } 737 }
@@ -867,8 +854,7 @@ int main(int argc, char * argv[]) @@ -867,8 +854,7 @@ int main(int argc, char * argv[])
867 854
868 load_codec_param(); 855 load_codec_param();
869 856
870 - MergeProcess mp;  
871 - mp.process_files("dest.ts"); 857 + process_files("dest.ts");
872 858
873 return 0; 859 return 0;
874 } 860 }