胡斌

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 + if (only_print){
  32 + printf("%s\n", cmd);
  33 + }
  34 + else
  35 + {
31 printf("run command:%s\n", cmd); 36 printf("run command:%s\n", cmd);
32 -  
33 - if (!only_print){  
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,37 +441,38 @@ int process_files(const char * output_dest_file) @@ -431,37 +441,38 @@ 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){ 474 +
  475 + if (audio.end_time > video.end_time){ //this video finish, to next video
465 sprintf(destfile, "%d_%s", nf, audio.name.c_str()); 476 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); 477 split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile);
467 tmp_files.push_back(destfile); 478 tmp_files.push_back(destfile);
@@ -472,7 +483,7 @@ int process_files(const char * output_dest_file) @@ -472,7 +483,7 @@ int process_files(const char * output_dest_file)
472 merged_files.push_back(destfile); 483 merged_files.push_back(destfile);
473 nf++; 484 nf++;
474 } 485 }
475 - else if (video.end_time - audio.end_time < 0.1){ 486 + else if (video.end_time - audio.end_time < 0.1){//just fine, this audio file finish
476 sprintf(destfile, "%d_%s", nf, audio.name.c_str()); 487 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); 488 split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);
478 tmp_files.push_back(destfile); 489 tmp_files.push_back(destfile);
@@ -480,10 +491,12 @@ int process_files(const char * output_dest_file) @@ -480,10 +491,12 @@ int process_files(const char * output_dest_file)
480 sprintf(destfile, "%d.ts", nf); 491 sprintf(destfile, "%d.ts", nf);
481 megre_audio_video(audio, nf, video, destfile); 492 megre_audio_video(audio, nf, video, destfile);
482 merged_files.push_back(destfile); 493 merged_files.push_back(destfile);
  494 + audio_start = audio.end_time + 0.1;//no audio left
483 nf++; 495 nf++;
  496 + nv++;//this video is used
484 break; 497 break;
485 } 498 }
486 - else { 499 + else { // this audio finish,add silence and/or next audio
487 sprintf(destfile, "%d_%s", nf, audio.name.c_str()); 500 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); 501 split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile);
489 vector<std::string > merge_audio_files; 502 vector<std::string > merge_audio_files;
@@ -493,19 +506,21 @@ int process_files(const char * output_dest_file) @@ -493,19 +506,21 @@ int process_files(const char * output_dest_file)
493 double silence_audio_start = audio.end_time; 506 double silence_audio_start = audio.end_time;
494 double silence_audio_end = video.end_time; 507 double silence_audio_end = video.end_time;
495 508
496 - for (; i + 1 < filesaudio.size(); i++){ 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
497 audio = filesaudio[i + 1]; 512 audio = filesaudio[i + 1];
498 - if (audio.start_time < video.end_time) { 513 + if (audio.start_time < video.end_time) {//next audio should split to fit the video
499 silence_audio_end = audio.start_time; 514 silence_audio_end = audio.start_time;
500 515
501 sprintf(destfile, "%d_%d_silence.aac", nf, i); 516 sprintf(destfile, "%d_%d_silence.aac", nf, i);
502 - split_audio(silence_aac_file,0, silence_audio_end - audio.end_time, destfile); 517 + split_audio(silence_aac_file, 0, silence_audio_end - silence_audio_start, destfile);
503 merge_audio_files.push_back(destfile); 518 merge_audio_files.push_back(destfile);
504 tmp_files.push_back(destfile); 519 tmp_files.push_back(destfile);
505 520
506 -  
507 - if (audio.end_time > video.end_time - 0.1 && audio.end_time < video.end_time + 0.1) { 521 + if (audio.end_time > video.end_time - 0.1 && audio.end_time < video.end_time + 0.1) {//just match
508 merge_audio_files.push_back(audio.name); 522 merge_audio_files.push_back(audio.name);
  523 + need_slilence = false;
509 i++; 524 i++;
510 break; 525 break;
511 } 526 }
@@ -514,15 +529,44 @@ int process_files(const char * output_dest_file) @@ -514,15 +529,44 @@ int process_files(const char * output_dest_file)
514 merge_audio_files.push_back(destfile); 529 merge_audio_files.push_back(destfile);
515 tmp_files.push_back(destfile); 530 tmp_files.push_back(destfile);
516 split_audio(audio.name.c_str(), 0, video.end_time - audio.start_time, 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;
  535 +
  536 + audio_start = audio.start_time;
  537 +
  538 + for (int j = i + 1; j < filesaudio.size(); j++){
  539 + filesaudio[j].start_time -= audio_start;
  540 + filesaudio[j].end_time -= audio_start;
  541 + }
  542 +
  543 + for (int j = nv; j < filesvideo.size(); j++) {
  544 + filesvideo[j].start_time -= audio_start;
  545 + filesvideo[j].end_time -= audio_start;
  546 + }
  547 + i++;
  548 + audio = filesaudio[i];
  549 + audio_start = cur_audio_start;
  550 +
  551 + to_next_video = true;
  552 +
517 break; 553 break;
518 } 554 }
519 - merge_audio_files.push_back(audio.name); 555 + merge_audio_files.push_back(audio.name);//whole audio should be appended
  556 + silence_audio_start = audio.end_time; //adjust the silence start
520 } 557 }
521 else { 558 else {
522 - break; 559 + break;//no need for next audio
523 } 560 }
524 } 561 }
525 562
  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);
  568 + }
  569 +
526 sprintf(audio_file, "%d_merged.aac", nf); 570 sprintf(audio_file, "%d_merged.aac", nf);
527 merge_audio_file(merge_audio_files, audio_file); 571 merge_audio_file(merge_audio_files, audio_file);
528 572
@@ -531,38 +575,19 @@ int process_files(const char * output_dest_file) @@ -531,38 +575,19 @@ int process_files(const char * output_dest_file)
531 megre_audio_video(audio_file, video.name.c_str(), destfile); 575 megre_audio_video(audio_file, video.name.c_str(), destfile);
532 merged_files.push_back(destfile); 576 merged_files.push_back(destfile);
533 nf++; 577 nf++;
534 - break;  
535 578
  579 + if (!to_next_video){
  580 + nv++;
  581 + break;
536 } 582 }
537 } 583 }
538 - else {  
539 -  
540 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
541 -  
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 -  
546 - audio_start = video.end_time;  
547 -  
548 - sprintf(destfile, "%d.ts", nf);  
549 - megre_audio_video(audio, nf, video, destfile);  
550 - merged_files.push_back(destfile);  
551 - nf++;  
552 } 584 }
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);  
556 -  
557 - audio_start = video.end_time;  
558 585
559 - sprintf(destfile, "%d.ts", nf);  
560 - megre_audio_video(audio, nf, video, destfile);  
561 - merged_files.push_back(destfile);  
562 - nf++; 586 + if (audio_start < audio.end_time){
563 587
564 sprintf(destfile, "%d_%s", nf, audio.name.c_str()); 588 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); 589 +
  590 + split_audio(audio.name.c_str(), audio_start, audio.end_time - audio_start, destfile);
566 tmp_files.push_back(destfile); 591 tmp_files.push_back(destfile);
567 592
568 sprintf(destfile, "%d.ts", nf); 593 sprintf(destfile, "%d.ts", nf);
@@ -572,8 +597,6 @@ int process_files(const char * output_dest_file) @@ -572,8 +597,6 @@ int process_files(const char * output_dest_file)
572 } 597 }
573 } 598 }
574 } 599 }
575 - }  
576 - }  
577 else{//only video 600 else{//only video
578 if (filesvideo.size() == 1){ 601 if (filesvideo.size() == 1){
579 fileinfo video = filesvideo[0]; 602 fileinfo video = filesvideo[0];