胡斌

seems ok

@@ -28,9 +28,12 @@ vector<fileinfo> media_files[2]; @@ -28,9 +28,12 @@ vector<fileinfo> media_files[2];
28 28
29 void run_shell_cmd(const char * cmd) 29 void run_shell_cmd(const char * cmd)
30 { 30 {
31 - printf("run command:%s\n", cmd);  
32 -  
33 - if (!only_print){ 31 + if (only_print){
  32 + printf("%s\n", cmd);
  33 + }
  34 + else
  35 + {
  36 + printf("run command:%s\n", cmd);
34 system(cmd); 37 system(cmd);
35 } 38 }
36 } 39 }
@@ -167,6 +170,13 @@ void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile @@ -167,6 +170,13 @@ void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile
167 run_shell_cmd(buf); 170 run_shell_cmd(buf);
168 } 171 }
169 172
  173 +void merge_audio_pic(const char * audio, const char * picfile, const char * destfile)
  174 +{
  175 + char buf[2048];
  176 + sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio, acodec_param, vcodec_param, destfile);
  177 + run_shell_cmd(buf);
  178 +}
  179 +
170 void merge_video_silence(fileinfo video, const char * aacfile, const char * destfile) 180 void merge_video_silence(fileinfo video, const char * aacfile, const char * destfile)
171 { 181 {
172 char buf[2048]; 182 char buf[2048];
@@ -400,7 +410,7 @@ int process_files(const char * output_dest_file) @@ -400,7 +410,7 @@ int process_files(const char * output_dest_file)
400 vector<string> tmp_files; 410 vector<string> tmp_files;
401 int nv = 0; 411 int nv = 0;
402 int nf = 0; 412 int nf = 0;
403 - char destfile[1024],audio_file[1024]; 413 + char destfile[1024],audio_file[1024],pic_file[1024];
404 char blank_pic_file[1024]; 414 char blank_pic_file[1024];
405 char silence_aac_file[1024]; 415 char silence_aac_file[1024];
406 416
@@ -431,146 +441,159 @@ int process_files(const char * output_dest_file) @@ -431,146 +441,159 @@ int process_files(const char * output_dest_file)
431 441
432 audio = filesaudio[i]; 442 audio = filesaudio[i];
433 443
434 - audio_start = 0; 444 + audio_start = 0;//for a new processing audio,the start is 0
435 445
436 for (; nv < filesvideo.size(); nv++) { 446 for (; nv < filesvideo.size(); nv++) {
437 fileinfo video = filesvideo[nv]; 447 fileinfo video = filesvideo[nv];
438 448
439 - if (video.start_time - audio_start > 0.100) {  
440 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
441 - if (video.start_time > audio.end_time){  
442 - split_audio(audio.name.c_str(), audio_start, audio.end_time - audio_start, destfile);  
443 - }  
444 - else{  
445 - split_audio(audio.name.c_str(), audio_start, video.start_time - audio_start, destfile); 449 + if (video.start_time - audio_start > 0.1) {//video is behand audio too much
  450 + sprintf(audio_file, "%d_%s", nf, audio.name.c_str());
  451 + if (video.start_time < audio.end_time - 0.1){
  452 + split_audio(audio.name.c_str(), audio_start, video.start_time - audio_start, audio_file);
446 audio_start = video.start_time; 453 audio_start = video.start_time;
  454 + tmp_files.push_back(audio_file);
  455 + }
  456 + else {
  457 + strcpy(audio_file, audio.name.c_str());
447 } 458 }
448 - tmp_files.push_back(destfile);  
449 459
450 - sprintf(destfile, "%s.jpg", video.name.c_str());  
451 - get_video_first_frame_jpeg(video, destfile);  
452 - tmp_files.push_back(destfile); 460 + sprintf(pic_file, "%s.jpg", video.name.c_str());
  461 + get_video_first_frame_jpeg(video, pic_file);
  462 + tmp_files.push_back(pic_file);
453 463
454 sprintf(destfile, "%d.ts", nf); 464 sprintf(destfile, "%d.ts", nf);
455 - merge_audio_pic(audio, nf, video, destfile); 465 + merge_audio_pic(audio_file, pic_file, destfile);
456 merged_files.push_back(destfile); 466 merged_files.push_back(destfile);
457 nf++; 467 nf++;
458 468
459 - if (video.start_time > audio.end_time){//to next audio 469 + if (video.start_time > audio.end_time){//all audio file no video, to next audio
  470 + audio_start = audio.end_time + 0.1;//no audio left
460 break; 471 break;
461 } 472 }
462 } 473 }
463 - if (nv != filesvideo.size() - 1) {// not the last one  
464 - if (audio.end_time > video.end_time){  
465 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
466 - split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile);  
467 - tmp_files.push_back(destfile);  
468 474
469 - audio_start = video.end_time;  
470 - sprintf(destfile, "%d.ts", nf);  
471 - megre_audio_video(audio, nf, video, destfile);  
472 - merged_files.push_back(destfile);  
473 - nf++;  
474 - }  
475 - else if (video.end_time - audio.end_time < 0.1){  
476 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
477 - split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);  
478 - tmp_files.push_back(destfile); 475 + if (audio.end_time > video.end_time){ //this video finish, to next video
  476 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
  477 + split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile);
  478 + tmp_files.push_back(destfile);
479 479
480 - sprintf(destfile, "%d.ts", nf);  
481 - megre_audio_video(audio, nf, video, destfile);  
482 - merged_files.push_back(destfile);  
483 - nf++;  
484 - break;  
485 - }  
486 - else {  
487 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
488 - split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);  
489 - vector<std::string > merge_audio_files;  
490 - merge_audio_files.push_back(destfile);  
491 - tmp_files.push_back(destfile);  
492 -  
493 - double silence_audio_start = audio.end_time;  
494 - double silence_audio_end = video.end_time; 480 + audio_start = video.end_time;
  481 + sprintf(destfile, "%d.ts", nf);
  482 + megre_audio_video(audio, nf, video, destfile);
  483 + merged_files.push_back(destfile);
  484 + nf++;
  485 + }
  486 + else if (video.end_time - audio.end_time < 0.1){//just fine, this audio file finish
  487 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
  488 + split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);
  489 + tmp_files.push_back(destfile);
  490 +
  491 + sprintf(destfile, "%d.ts", nf);
  492 + megre_audio_video(audio, nf, video, destfile);
  493 + merged_files.push_back(destfile);
  494 + audio_start = audio.end_time + 0.1;//no audio left
  495 + nf++;
  496 + nv++;//this video is used
  497 + break;
  498 + }
  499 + else { // this audio finish,add silence and/or next audio
  500 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
  501 + split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);
  502 + vector<std::string > merge_audio_files;
  503 + merge_audio_files.push_back(destfile);
  504 + tmp_files.push_back(destfile);
  505 +
  506 + double silence_audio_start = audio.end_time;
  507 + double silence_audio_end = video.end_time;
  508 +
  509 + bool need_slilence = true;
  510 + bool to_next_video = false;
  511 + for (; i + 1 < filesaudio.size(); i++){//since video is not finished,try find next audio
  512 + audio = filesaudio[i + 1];
  513 + if (audio.start_time < video.end_time) {//next audio should split to fit the video
  514 + silence_audio_end = audio.start_time;
495 515
496 - for (; i + 1 < filesaudio.size(); i++){  
497 - audio = filesaudio[i + 1];  
498 - if (audio.start_time < video.end_time) {  
499 - silence_audio_end = audio.start_time; 516 + sprintf(destfile, "%d_%d_silence.aac", nf, i);
  517 + split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile);
  518 + merge_audio_files.push_back(destfile);
  519 + tmp_files.push_back(destfile);
500 520
501 - sprintf(destfile, "%d_%d_silence.aac", nf, i);  
502 - split_audio(silence_aac_file,0, silence_audio_end - audio.end_time, destfile); 521 + if (audio.end_time > video.end_time - 0.1 && audio.end_time < video.end_time + 0.1) {//just match
  522 + merge_audio_files.push_back(audio.name);
  523 + need_slilence = false;
  524 + i++;
  525 + break;
  526 + }
  527 + if (audio.end_time > video.end_time){
  528 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
503 merge_audio_files.push_back(destfile); 529 merge_audio_files.push_back(destfile);
504 tmp_files.push_back(destfile); 530 tmp_files.push_back(destfile);
  531 + split_audio(audio.name.c_str(), 0, video.end_time - audio.start_time, destfile);
  532 + need_slilence = false;
  533 + //adjust timecode for the audio is part left
  534 + float cur_audio_start = video.end_time - audio.start_time;
505 535
  536 + audio_start = audio.start_time;
506 537
507 - if (audio.end_time > video.end_time - 0.1 && audio.end_time < video.end_time + 0.1) {  
508 - merge_audio_files.push_back(audio.name);  
509 - i++;  
510 - break; 538 + for (int j = i + 1; j < filesaudio.size(); j++){
  539 + filesaudio[j].start_time -= audio_start;
  540 + filesaudio[j].end_time -= audio_start;
511 } 541 }
512 - if (audio.end_time > video.end_time){  
513 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
514 - merge_audio_files.push_back(destfile);  
515 - tmp_files.push_back(destfile);  
516 - split_audio(audio.name.c_str(), 0, video.end_time - audio.start_time, destfile);  
517 - break; 542 +
  543 + for (int j = nv; j < filesvideo.size(); j++) {
  544 + filesvideo[j].start_time -= audio_start;
  545 + filesvideo[j].end_time -= audio_start;
518 } 546 }
519 - merge_audio_files.push_back(audio.name);  
520 - }  
521 - else { 547 + i++;
  548 + audio = filesaudio[i];
  549 + audio_start = cur_audio_start;
  550 +
  551 + to_next_video = true;
  552 +
522 break; 553 break;
523 } 554 }
  555 + merge_audio_files.push_back(audio.name);//whole audio should be appended
  556 + silence_audio_start = audio.end_time; //adjust the silence start
524 } 557 }
  558 + else {
  559 + break;//no need for next audio
  560 + }
  561 + }
525 562
526 - sprintf(audio_file, "%d_merged.aac", nf);  
527 - merge_audio_file(merge_audio_files, audio_file);  
528 -  
529 -  
530 - sprintf(destfile, "%d.ts", nf);  
531 - megre_audio_video(audio_file, video.name.c_str(), destfile);  
532 - merged_files.push_back(destfile);  
533 - nf++;  
534 - break;  
535 - 563 + if (need_slilence) {
  564 + sprintf(destfile, "%d_silence.aac", nf);
  565 + split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile);
  566 + merge_audio_files.push_back(destfile);
  567 + tmp_files.push_back(destfile);
536 } 568 }
537 - }  
538 - else {  
539 569
540 - sprintf(destfile, "%d_%s", nf, audio.name.c_str()); 570 + sprintf(audio_file, "%d_merged.aac", nf);
  571 + merge_audio_file(merge_audio_files, audio_file);
541 572
542 - if (audio.end_time - video.end_time < 1.0) {  
543 - split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);  
544 - tmp_files.push_back(destfile);  
545 573
546 - audio_start = video.end_time; 574 + sprintf(destfile, "%d.ts", nf);
  575 + megre_audio_video(audio_file, video.name.c_str(), destfile);
  576 + merged_files.push_back(destfile);
  577 + nf++;
547 578
548 - sprintf(destfile, "%d.ts", nf);  
549 - megre_audio_video(audio, nf, video, destfile);  
550 - merged_files.push_back(destfile);  
551 - nf++; 579 + if (!to_next_video){
  580 + nv++;
  581 + break;
552 } 582 }
553 - else{  
554 - split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile);  
555 - tmp_files.push_back(destfile); 583 + }
  584 + }
556 585
557 - audio_start = video.end_time; 586 + if (audio_start < audio.end_time){
558 587
559 - sprintf(destfile, "%d.ts", nf);  
560 - megre_audio_video(audio, nf, video, destfile);  
561 - merged_files.push_back(destfile);  
562 - nf++; 588 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
563 589
564 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
565 - split_audio(audio.name.c_str(), video.end_time, audio.end_time - video.end_time, destfile);  
566 - tmp_files.push_back(destfile); 590 + split_audio(audio.name.c_str(), audio_start, audio.end_time - audio_start, destfile);
  591 + tmp_files.push_back(destfile);
567 592
568 - sprintf(destfile, "%d.ts", nf);  
569 - merge_audio_pic(audio, nf, blank_pic_file, destfile);  
570 - merged_files.push_back(destfile);  
571 - nf++;  
572 - }  
573 - } 593 + sprintf(destfile, "%d.ts", nf);
  594 + merge_audio_pic(audio, nf, blank_pic_file, destfile);
  595 + merged_files.push_back(destfile);
  596 + nf++;
574 } 597 }
575 } 598 }
576 } 599 }