正在显示
1 个修改的文件
包含
138 行增加
和
65 行删除
| @@ -402,18 +402,44 @@ int merge_audio_file(vector<string> & files, const char * dest) | @@ -402,18 +402,44 @@ int merge_audio_file(vector<string> & files, const char * dest) | ||
| 402 | return 0; | 402 | return 0; |
| 403 | } | 403 | } |
| 404 | 404 | ||
| 405 | -int process_files(const char * output_dest_file) | ||
| 406 | -{ | ||
| 407 | - vector<fileinfo> & filesaudio = media_files[type_audio]; | ||
| 408 | - vector<fileinfo> & filesvideo = media_files[type_video]; | 405 | +class MergeProcess{ |
| 406 | +public: | ||
| 407 | + MergeProcess(vector<fileinfo> & a, vector<fileinfo> & v); | ||
| 408 | + int process_files(const char * output_dest_file); | ||
| 409 | + | ||
| 410 | +protected: | ||
| 411 | + void init(); | ||
| 412 | + void adjust_va_timestamp(); | ||
| 413 | + void merge_left_audio(); | ||
| 414 | + int process_video_ahead_of_audio(); | ||
| 415 | + int process_video_behind_audio(); | ||
| 416 | + int process_video_align_audio(); | ||
| 417 | + int process_video_loop(); | ||
| 418 | + int process_va(); | ||
| 419 | + | ||
| 420 | +protected: | ||
| 421 | + vector<fileinfo> & filesaudio; | ||
| 422 | + vector<fileinfo> & filesvideo; | ||
| 409 | vector<string > merged_files; | 423 | vector<string > merged_files; |
| 410 | vector<string> tmp_files; | 424 | vector<string> tmp_files; |
| 411 | - int nv = 0; | ||
| 412 | - int nf = 0; | ||
| 413 | - char destfile[1024],audio_file[1024],pic_file[1024]; | 425 | + int nv; // the index of processing video file |
| 426 | + int nf;//the index of processing target merged ts | ||
| 427 | + char destfile[1024], audio_file[1024], pic_file[1024]; | ||
| 414 | char blank_pic_file[1024]; | 428 | char blank_pic_file[1024]; |
| 415 | char silence_aac_file[1024]; | 429 | char silence_aac_file[1024]; |
| 430 | + int audio_index; | ||
| 431 | + int audio_start; | ||
| 432 | + fileinfo audio; | ||
| 433 | + fileinfo video; | ||
| 434 | +}; | ||
| 435 | + | ||
| 436 | +MergeProcess::MergeProcess(vector<fileinfo> & a, vector<fileinfo> & v) :filesaudio(a), filesvideo(v) | ||
| 437 | +{ | ||
| 438 | + init(); | ||
| 439 | +} | ||
| 416 | 440 | ||
| 441 | +void MergeProcess::init() | ||
| 442 | +{ | ||
| 417 | strcpy(blank_pic_file, cfg_path); | 443 | strcpy(blank_pic_file, cfg_path); |
| 418 | strcat(blank_pic_file, "blank.jpg"); | 444 | strcat(blank_pic_file, "blank.jpg"); |
| 419 | 445 | ||
| @@ -422,16 +448,17 @@ int process_files(const char * output_dest_file) | @@ -422,16 +448,17 @@ int process_files(const char * output_dest_file) | ||
| 422 | 448 | ||
| 423 | check_audio_duration(); | 449 | check_audio_duration(); |
| 424 | get_duration_from_video_file(); | 450 | get_duration_from_video_file(); |
| 425 | - //don't split video, for a video, using merged audios to mix with it | ||
| 426 | - //for audio, mix with video or jpg | 451 | + nv = 0; |
| 452 | + nf = 0; | ||
| 453 | + audio_index = 0; | ||
| 454 | +} | ||
| 427 | 455 | ||
| 428 | - if (filesvideo.size()) {//has video files | ||
| 429 | - if (filesaudio.size()){ | ||
| 430 | - for (int i = 0; i < filesaudio.size(); i++){ // | ||
| 431 | - fileinfo audio = filesaudio[i]; | 456 | +void MergeProcess::adjust_va_timestamp() |
| 457 | +{ | ||
| 458 | + fileinfo audio = filesaudio[audio_index]; | ||
| 432 | float audio_start = audio.start_time; | 459 | float audio_start = audio.start_time; |
| 433 | 460 | ||
| 434 | - for (int j = i; j < filesaudio.size(); j++){ | 461 | + for (int j = audio_index; j < filesaudio.size(); j++){ |
| 435 | filesaudio[j].start_time -= audio_start; | 462 | filesaudio[j].start_time -= audio_start; |
| 436 | filesaudio[j].end_time -= audio_start; | 463 | filesaudio[j].end_time -= audio_start; |
| 437 | } | 464 | } |
| @@ -440,16 +467,34 @@ int process_files(const char * output_dest_file) | @@ -440,16 +467,34 @@ int process_files(const char * output_dest_file) | ||
| 440 | filesvideo[j].start_time -= audio_start; | 467 | filesvideo[j].start_time -= audio_start; |
| 441 | filesvideo[j].end_time -= audio_start; | 468 | filesvideo[j].end_time -= audio_start; |
| 442 | } | 469 | } |
| 470 | +} | ||
| 443 | 471 | ||
| 444 | - audio = filesaudio[i]; | ||
| 445 | 472 | ||
| 446 | - audio_start = 0;//for a new processing audio,the start is 0 | 473 | +void MergeProcess::merge_left_audio() |
| 474 | +{ | ||
| 475 | + if (audio_start < audio.end_time){ | ||
| 476 | + sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | ||
| 477 | + split_audio(audio.name.c_str(), audio_start, audio.end_time - audio_start, destfile); | ||
| 478 | + tmp_files.push_back(destfile); | ||
| 447 | 479 | ||
| 448 | - for (; nv < filesvideo.size(); nv++) { | 480 | + sprintf(destfile, "%d.ts", nf); |
| 481 | + if (nv < filesvideo.size()) { | ||
| 449 | fileinfo video = filesvideo[nv]; | 482 | fileinfo video = filesvideo[nv]; |
| 483 | + sprintf(pic_file, "%s.jpg", video.name.c_str()); | ||
| 484 | + get_video_first_frame_jpeg(video, pic_file); | ||
| 485 | + tmp_files.push_back(pic_file); | ||
| 486 | + } | ||
| 487 | + else { | ||
| 488 | + strcpy(pic_file, blank_pic_file); | ||
| 489 | + } | ||
| 490 | + merge_audio_pic(audio, nf, pic_file, destfile); | ||
| 491 | + merged_files.push_back(destfile); | ||
| 492 | + nf++; | ||
| 493 | + } | ||
| 494 | +} | ||
| 450 | 495 | ||
| 451 | - if (video.start_time < audio_start - 0.1) | ||
| 452 | - {//video is much more ahead of audio,try padding silence first | 496 | +int MergeProcess::process_video_ahead_of_audio() |
| 497 | +{ | ||
| 453 | if (video.end_time < audio_start + 0.1) { | 498 | if (video.end_time < audio_start + 0.1) { |
| 454 | sprintf(destfile, "%d_silence.aac", nf); | 499 | sprintf(destfile, "%d_silence.aac", nf); |
| 455 | split_audio(silence_aac_file, 0, video.end_time - video.start_time, destfile); | 500 | split_audio(silence_aac_file, 0, video.end_time - video.start_time, destfile); |
| @@ -458,7 +503,7 @@ int process_files(const char * output_dest_file) | @@ -458,7 +503,7 @@ int process_files(const char * output_dest_file) | ||
| 458 | merge_audio_video(destfile, video.name.c_str(), destfile); | 503 | merge_audio_video(destfile, video.name.c_str(), destfile); |
| 459 | merged_files.push_back(destfile); | 504 | merged_files.push_back(destfile); |
| 460 | nf++; | 505 | nf++; |
| 461 | - continue; //for next video | 506 | + return 0; //for next video |
| 462 | } | 507 | } |
| 463 | else { | 508 | else { |
| 464 | // combine a audio file for the video | 509 | // combine a audio file for the video |
| @@ -490,12 +535,12 @@ int process_files(const char * output_dest_file) | @@ -490,12 +535,12 @@ int process_files(const char * output_dest_file) | ||
| 490 | else { | 535 | else { |
| 491 | merge_audio_files.push_back(audio.name); | 536 | merge_audio_files.push_back(audio.name); |
| 492 | 537 | ||
| 493 | - for (; i + 1 < filesaudio.size(); i++){//since video is not finished,try find next audio | ||
| 494 | - audio = filesaudio[i + 1]; | 538 | + for (; audio_index + 1 < filesaudio.size(); audio_index++){//since video is not finished,try find next audio |
| 539 | + audio = filesaudio[audio_index + 1]; | ||
| 495 | if (audio.start_time < video.end_time) {//next audio should split to fit the video | 540 | if (audio.start_time < video.end_time) {//next audio should split to fit the video |
| 496 | silence_audio_end = audio.start_time; | 541 | silence_audio_end = audio.start_time; |
| 497 | 542 | ||
| 498 | - sprintf(destfile, "%d_%d_silence.aac", nf, i); | 543 | + sprintf(destfile, "%d_%d_silence.aac", nf, audio_index); |
| 499 | split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile); | 544 | split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile); |
| 500 | merge_audio_files.push_back(destfile); | 545 | merge_audio_files.push_back(destfile); |
| 501 | tmp_files.push_back(destfile); | 546 | tmp_files.push_back(destfile); |
| @@ -504,7 +549,7 @@ int process_files(const char * output_dest_file) | @@ -504,7 +549,7 @@ int process_files(const char * output_dest_file) | ||
| 504 | merge_audio_files.push_back(audio.name); | 549 | merge_audio_files.push_back(audio.name); |
| 505 | need_append_silence = false; | 550 | need_append_silence = false; |
| 506 | audio_start = audio.end_time + 0.1; | 551 | audio_start = audio.end_time + 0.1; |
| 507 | - i++;//this audio is used | 552 | + audio_index++;//this audio is used |
| 508 | break; | 553 | break; |
| 509 | } | 554 | } |
| 510 | if (audio.end_time > video.end_time){ | 555 | if (audio.end_time > video.end_time){ |
| @@ -518,7 +563,7 @@ int process_files(const char * output_dest_file) | @@ -518,7 +563,7 @@ int process_files(const char * output_dest_file) | ||
| 518 | 563 | ||
| 519 | audio_start = audio.start_time; | 564 | audio_start = audio.start_time; |
| 520 | 565 | ||
| 521 | - for (int j = i + 1; j < filesaudio.size(); j++){ | 566 | + for (int j = audio_index + 1; j < filesaudio.size(); j++){ |
| 522 | filesaudio[j].start_time -= audio_start; | 567 | filesaudio[j].start_time -= audio_start; |
| 523 | filesaudio[j].end_time -= audio_start; | 568 | filesaudio[j].end_time -= audio_start; |
| 524 | } | 569 | } |
| @@ -527,8 +572,8 @@ int process_files(const char * output_dest_file) | @@ -527,8 +572,8 @@ int process_files(const char * output_dest_file) | ||
| 527 | filesvideo[j].start_time -= audio_start; | 572 | filesvideo[j].start_time -= audio_start; |
| 528 | filesvideo[j].end_time -= audio_start; | 573 | filesvideo[j].end_time -= audio_start; |
| 529 | } | 574 | } |
| 530 | - i++; | ||
| 531 | - audio = filesaudio[i]; | 575 | + audio_index++; |
| 576 | + audio = filesaudio[audio_index]; | ||
| 532 | audio_start = cur_audio_start; | 577 | audio_start = cur_audio_start; |
| 533 | 578 | ||
| 534 | to_next_video = true; | 579 | to_next_video = true; |
| @@ -562,13 +607,14 @@ int process_files(const char * output_dest_file) | @@ -562,13 +607,14 @@ int process_files(const char * output_dest_file) | ||
| 562 | 607 | ||
| 563 | if (!to_next_video){ | 608 | if (!to_next_video){ |
| 564 | nv++;//this video is processed | 609 | nv++;//this video is processed |
| 565 | - break; | 610 | + return 1; |
| 566 | } | 611 | } |
| 567 | }//end need combine | 612 | }//end need combine |
| 568 | - }//end video is ahead of audio | ||
| 569 | - //-----VS----- | ||
| 570 | - //AS---------- | ||
| 571 | - if (video.start_time - audio_start > 0.1) {//video is behind audio too much | 613 | + return 0; |
| 614 | +} | ||
| 615 | + | ||
| 616 | +int MergeProcess::process_video_behind_audio() | ||
| 617 | +{ | ||
| 572 | sprintf(audio_file, "%d_%s", nf, audio.name.c_str()); | 618 | sprintf(audio_file, "%d_%s", nf, audio.name.c_str()); |
| 573 | if (video.start_time < audio.end_time - 0.1){ | 619 | if (video.start_time < audio.end_time - 0.1){ |
| 574 | split_audio(audio.name.c_str(), audio_start, video.start_time - audio_start, audio_file); | 620 | split_audio(audio.name.c_str(), audio_start, video.start_time - audio_start, audio_file); |
| @@ -590,13 +636,13 @@ int process_files(const char * output_dest_file) | @@ -590,13 +636,13 @@ int process_files(const char * output_dest_file) | ||
| 590 | 636 | ||
| 591 | if (video.start_time >= audio.end_time - 0.1){//all audio file no video, to next audio | 637 | if (video.start_time >= audio.end_time - 0.1){//all audio file no video, to next audio |
| 592 | audio_start = audio.end_time + 0.1;//no audio left | 638 | audio_start = audio.end_time + 0.1;//no audio left |
| 593 | - break; | ||
| 594 | - } | 639 | + return 1; |
| 595 | } | 640 | } |
| 596 | - //----AS-------- | ||
| 597 | - //----VS-------- | ||
| 598 | - else if (audio_start - video.start_time < 0.1){ | 641 | + return 0; |
| 642 | +} | ||
| 599 | 643 | ||
| 644 | +int MergeProcess::process_video_align_audio() | ||
| 645 | +{ | ||
| 600 | if (audio.end_time > video.end_time){ //this video finish, to next video | 646 | if (audio.end_time > video.end_time){ //this video finish, to next video |
| 601 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | 647 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 602 | split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile); | 648 | split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile); |
| @@ -619,7 +665,7 @@ int process_files(const char * output_dest_file) | @@ -619,7 +665,7 @@ int process_files(const char * output_dest_file) | ||
| 619 | audio_start = audio.end_time + 0.1;//no audio left | 665 | audio_start = audio.end_time + 0.1;//no audio left |
| 620 | nf++; | 666 | nf++; |
| 621 | nv++;//this video is used | 667 | nv++;//this video is used |
| 622 | - break; | 668 | + return 1; |
| 623 | } | 669 | } |
| 624 | else { // this audio finish,add silence and/or next audio | 670 | else { // this audio finish,add silence and/or next audio |
| 625 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | 671 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| @@ -635,12 +681,12 @@ int process_files(const char * output_dest_file) | @@ -635,12 +681,12 @@ int process_files(const char * output_dest_file) | ||
| 635 | 681 | ||
| 636 | bool need_silence = true; | 682 | bool need_silence = true; |
| 637 | bool to_next_video = false; | 683 | bool to_next_video = false; |
| 638 | - for (; i + 1 < filesaudio.size(); i++){//since video is not finished,try find next audio | ||
| 639 | - audio = filesaudio[i + 1]; | 684 | + for (; audio_index + 1 < filesaudio.size(); audio_index++){//since video is not finished,try find next audio |
| 685 | + audio = filesaudio[audio_index + 1]; | ||
| 640 | if (audio.start_time < video.end_time) {//next audio should split to fit the video | 686 | if (audio.start_time < video.end_time) {//next audio should split to fit the video |
| 641 | silence_audio_end = audio.start_time; | 687 | silence_audio_end = audio.start_time; |
| 642 | 688 | ||
| 643 | - sprintf(destfile, "%d_%d_silence.aac", nf, i); | 689 | + sprintf(destfile, "%d_%d_silence.aac", nf, audio_index); |
| 644 | split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile); | 690 | split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile); |
| 645 | merge_audio_files.push_back(destfile); | 691 | merge_audio_files.push_back(destfile); |
| 646 | tmp_files.push_back(destfile); | 692 | tmp_files.push_back(destfile); |
| @@ -649,7 +695,7 @@ int process_files(const char * output_dest_file) | @@ -649,7 +695,7 @@ int process_files(const char * output_dest_file) | ||
| 649 | merge_audio_files.push_back(audio.name); | 695 | merge_audio_files.push_back(audio.name); |
| 650 | need_silence = false; | 696 | need_silence = false; |
| 651 | audio_start = audio.end_time + 0.1; | 697 | audio_start = audio.end_time + 0.1; |
| 652 | - i++; | 698 | + audio_index++; |
| 653 | break; | 699 | break; |
| 654 | } | 700 | } |
| 655 | if (audio.end_time > video.end_time){ | 701 | if (audio.end_time > video.end_time){ |
| @@ -663,7 +709,7 @@ int process_files(const char * output_dest_file) | @@ -663,7 +709,7 @@ int process_files(const char * output_dest_file) | ||
| 663 | 709 | ||
| 664 | audio_start = audio.start_time; | 710 | audio_start = audio.start_time; |
| 665 | 711 | ||
| 666 | - for (int j = i + 1; j < filesaudio.size(); j++){ | 712 | + for (int j = audio_index + 1; j < filesaudio.size(); j++){ |
| 667 | filesaudio[j].start_time -= audio_start; | 713 | filesaudio[j].start_time -= audio_start; |
| 668 | filesaudio[j].end_time -= audio_start; | 714 | filesaudio[j].end_time -= audio_start; |
| 669 | } | 715 | } |
| @@ -672,8 +718,8 @@ int process_files(const char * output_dest_file) | @@ -672,8 +718,8 @@ int process_files(const char * output_dest_file) | ||
| 672 | filesvideo[j].start_time -= audio_start; | 718 | filesvideo[j].start_time -= audio_start; |
| 673 | filesvideo[j].end_time -= audio_start; | 719 | filesvideo[j].end_time -= audio_start; |
| 674 | } | 720 | } |
| 675 | - i++; | ||
| 676 | - audio = filesaudio[i]; | 721 | + audio_index++; |
| 722 | + audio = filesaudio[audio_index]; | ||
| 677 | audio_start = cur_audio_start; | 723 | audio_start = cur_audio_start; |
| 678 | 724 | ||
| 679 | to_next_video = true; | 725 | to_next_video = true; |
| @@ -707,32 +753,59 @@ int process_files(const char * output_dest_file) | @@ -707,32 +753,59 @@ int process_files(const char * output_dest_file) | ||
| 707 | 753 | ||
| 708 | if (!to_next_video){ | 754 | if (!to_next_video){ |
| 709 | nv++; | 755 | nv++; |
| 710 | - break; | ||
| 711 | - } | ||
| 712 | - } | 756 | + return 1; |
| 713 | } | 757 | } |
| 714 | } | 758 | } |
| 759 | + return 0; | ||
| 760 | +} | ||
| 715 | 761 | ||
| 716 | - if (audio_start < audio.end_time){ | ||
| 717 | - sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | ||
| 718 | - split_audio(audio.name.c_str(), audio_start, audio.end_time - audio_start, destfile); | ||
| 719 | - tmp_files.push_back(destfile); | 762 | +int MergeProcess::process_video_loop() |
| 763 | +{ | ||
| 764 | + for (; nv < filesvideo.size(); nv++) { | ||
| 765 | + video = filesvideo[nv]; | ||
| 720 | 766 | ||
| 721 | - sprintf(destfile, "%d.ts", nf); | ||
| 722 | - if (nv < filesvideo.size()) { | ||
| 723 | - fileinfo video = filesvideo[nv]; | ||
| 724 | - sprintf(pic_file, "%s.jpg", video.name.c_str()); | ||
| 725 | - get_video_first_frame_jpeg(video, pic_file); | ||
| 726 | - tmp_files.push_back(pic_file); | 767 | + if (video.start_time < audio_start - 0.1) |
| 768 | + {//video is much more ahead of audio,try padding silence first | ||
| 769 | + if (process_video_ahead_of_audio()) | ||
| 770 | + break; | ||
| 771 | + }//end video is ahead of audio | ||
| 772 | + if (video.start_time - audio_start > 0.1) {//video is behind audio too much | ||
| 773 | + if (process_video_behind_audio()) | ||
| 774 | + break; | ||
| 727 | } | 775 | } |
| 728 | - else { | ||
| 729 | - strcpy(pic_file, blank_pic_file); | 776 | + else if (audio_start - video.start_time < 0.1){ |
| 777 | + if (process_video_align_audio()) | ||
| 778 | + break; | ||
| 730 | } | 779 | } |
| 731 | - merge_audio_pic(audio, nf, pic_file, destfile); | ||
| 732 | - merged_files.push_back(destfile); | ||
| 733 | - nf++; | ||
| 734 | } | 780 | } |
| 781 | + return 0; | ||
| 782 | +} | ||
| 783 | + | ||
| 784 | +int MergeProcess::process_va() | ||
| 785 | +{ | ||
| 786 | + for (audio_index = 0; audio_index < filesaudio.size(); audio_index++){ // | ||
| 787 | + | ||
| 788 | + adjust_va_timestamp(); | ||
| 789 | + | ||
| 790 | + audio = filesaudio[audio_index]; | ||
| 791 | + audio_start = 0;//for a new processing audio,the start is 0 | ||
| 792 | + | ||
| 793 | + process_video_loop(); | ||
| 794 | + | ||
| 795 | + merge_left_audio(); | ||
| 735 | } | 796 | } |
| 797 | + return 0; | ||
| 798 | +} | ||
| 799 | + | ||
| 800 | + | ||
| 801 | +int MergeProcess::process_files(const char * output_dest_file) | ||
| 802 | +{ | ||
| 803 | + //don't split video, for a video, using merged audios to mix with it | ||
| 804 | + //for audio, mix with video or jpg | ||
| 805 | + | ||
| 806 | + if (filesvideo.size()) {//has video files | ||
| 807 | + if (filesaudio.size()){ | ||
| 808 | + process_va(); //process the case both audio and video files exist | ||
| 736 | } | 809 | } |
| 737 | else{//only video | 810 | else{//only video |
| 738 | if (filesvideo.size() == 1){ | 811 | if (filesvideo.size() == 1){ |
| @@ -772,7 +845,6 @@ int process_files(const char * output_dest_file) | @@ -772,7 +845,6 @@ int process_files(const char * output_dest_file) | ||
| 772 | 845 | ||
| 773 | adjust_dest_timecode("m.ts", output_dest_file); | 846 | adjust_dest_timecode("m.ts", output_dest_file); |
| 774 | 847 | ||
| 775 | - | ||
| 776 | if (!keep_tmp_files) { | 848 | if (!keep_tmp_files) { |
| 777 | removefiles(tmp_files); | 849 | removefiles(tmp_files); |
| 778 | removefiles(merged_files); | 850 | removefiles(merged_files); |
| @@ -866,7 +938,8 @@ int main(int argc, char * argv[]) | @@ -866,7 +938,8 @@ int main(int argc, char * argv[]) | ||
| 866 | 938 | ||
| 867 | load_codec_param(); | 939 | load_codec_param(); |
| 868 | 940 | ||
| 869 | - process_files("dest.ts"); | 941 | + MergeProcess mp(media_files[type_audio], media_files[type_video]); |
| 942 | + mp.process_files("dest.ts"); | ||
| 870 | 943 | ||
| 871 | return 0; | 944 | return 0; |
| 872 | } | 945 | } |
-
请 注册 或 登录 后发表评论