正在显示
1 个修改的文件
包含
113 行增加
和
17 行删除
| @@ -132,10 +132,10 @@ void split(string str, string separator, vector<string> &result, bool includeEmp | @@ -132,10 +132,10 @@ void split(string str, string separator, vector<string> &result, bool includeEmp | ||
| 132 | result.push_back(str.substr(lastPosition, string::npos)); | 132 | result.push_back(str.substr(lastPosition, string::npos)); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | -void split_audio(fileinfo audio, float audio_start, float duration, char * destfile) | 135 | +void split_audio(const char * audiofile, float audio_start, float duration, char * destfile) |
| 136 | { | 136 | { |
| 137 | char buf[2048]; | 137 | char buf[2048]; |
| 138 | - sprintf(buf, "ffmpeg -y -i %s -ss %.3f -t %.3f %s %s", audio.name.c_str(), audio_start, duration, acodec_param, destfile); | 138 | + sprintf(buf, "ffmpeg -y -i %s -ss %.3f -t %.3f %s %s", audiofile, audio_start, duration, acodec_param, destfile); |
| 139 | run_shell_cmd(buf); | 139 | run_shell_cmd(buf); |
| 140 | } | 140 | } |
| 141 | 141 | ||
| @@ -181,6 +181,13 @@ void megre_audio_video(fileinfo audio, int nf, fileinfo video, const char * dest | @@ -181,6 +181,13 @@ void megre_audio_video(fileinfo audio, int nf, fileinfo video, const char * dest | ||
| 181 | run_shell_cmd(buf); | 181 | run_shell_cmd(buf); |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | +void megre_audio_video(const char * audio, const char * video, const char * destfile) | ||
| 185 | +{ | ||
| 186 | + char buf[2048]; | ||
| 187 | + sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile); | ||
| 188 | + run_shell_cmd(buf); | ||
| 189 | +} | ||
| 190 | + | ||
| 184 | void concate_files(vector<string > merged_files, const char * destfile) | 191 | void concate_files(vector<string > merged_files, const char * destfile) |
| 185 | { | 192 | { |
| 186 | char buf[2048]; | 193 | char buf[2048]; |
| @@ -359,6 +366,32 @@ void check_audio_duration() | @@ -359,6 +366,32 @@ void check_audio_duration() | ||
| 359 | only_print = tmp; | 366 | only_print = tmp; |
| 360 | } | 367 | } |
| 361 | 368 | ||
| 369 | +int merge_audio_file(vector<string> & files, const char * dest) | ||
| 370 | +{ | ||
| 371 | + char buf[2048],tsfile[1024]; | ||
| 372 | + vector<string> tsfiles; | ||
| 373 | + | ||
| 374 | + for (int i = 0; i < files.size(); i++){ | ||
| 375 | + strcpy(tsfile, files[i].c_str()); | ||
| 376 | + strcat(tsfile, ".ts"); | ||
| 377 | + tsfiles.push_back(tsfile); | ||
| 378 | + | ||
| 379 | + sprintf(buf, "ffmpeg -y -i %s -acodec copy -vn %s", files[i].c_str(), tsfile); | ||
| 380 | + run_shell_cmd(buf); | ||
| 381 | + } | ||
| 382 | + | ||
| 383 | + sprintf(tsfile, "m_%s.ts", dest); | ||
| 384 | + concate_files(tsfiles, tsfile); | ||
| 385 | + | ||
| 386 | + adjust_dest_timecode(tsfile, dest); | ||
| 387 | + | ||
| 388 | + if (!keep_tmp_files){ | ||
| 389 | + tsfiles.push_back(tsfile); | ||
| 390 | + removefiles(tsfiles); | ||
| 391 | + } | ||
| 392 | + return 0; | ||
| 393 | +} | ||
| 394 | + | ||
| 362 | int process_files(const char * output_dest_file) | 395 | int process_files(const char * output_dest_file) |
| 363 | { | 396 | { |
| 364 | vector<fileinfo> & filesaudio = media_files[type_audio]; | 397 | vector<fileinfo> & filesaudio = media_files[type_audio]; |
| @@ -367,7 +400,7 @@ int process_files(const char * output_dest_file) | @@ -367,7 +400,7 @@ int process_files(const char * output_dest_file) | ||
| 367 | vector<string> tmp_files; | 400 | vector<string> tmp_files; |
| 368 | int nv = 0; | 401 | int nv = 0; |
| 369 | int nf = 0; | 402 | int nf = 0; |
| 370 | - char destfile[1024]; | 403 | + char destfile[1024],audio_file[1024]; |
| 371 | char blank_pic_file[1024]; | 404 | char blank_pic_file[1024]; |
| 372 | char silence_aac_file[1024]; | 405 | char silence_aac_file[1024]; |
| 373 | 406 | ||
| @@ -406,15 +439,14 @@ int process_files(const char * output_dest_file) | @@ -406,15 +439,14 @@ int process_files(const char * output_dest_file) | ||
| 406 | if (video.start_time - audio_start > 0.100) { | 439 | if (video.start_time - audio_start > 0.100) { |
| 407 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | 440 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 408 | if (video.start_time > audio.end_time){ | 441 | if (video.start_time > audio.end_time){ |
| 409 | - split_audio(audio, audio_start, audio.end_time - audio_start, destfile); | 442 | + split_audio(audio.name.c_str(), audio_start, audio.end_time - audio_start, destfile); |
| 410 | } | 443 | } |
| 411 | else{ | 444 | else{ |
| 412 | - split_audio(audio, audio_start, video.start_time - audio_start, destfile); | 445 | + split_audio(audio.name.c_str(), audio_start, video.start_time - audio_start, destfile); |
| 413 | audio_start = video.start_time; | 446 | audio_start = video.start_time; |
| 414 | } | 447 | } |
| 415 | tmp_files.push_back(destfile); | 448 | tmp_files.push_back(destfile); |
| 416 | 449 | ||
| 417 | - | ||
| 418 | sprintf(destfile, "%s.jpg", video.name.c_str()); | 450 | sprintf(destfile, "%s.jpg", video.name.c_str()); |
| 419 | get_video_first_frame_jpeg(video, destfile); | 451 | get_video_first_frame_jpeg(video, destfile); |
| 420 | tmp_files.push_back(destfile); | 452 | tmp_files.push_back(destfile); |
| @@ -429,22 +461,86 @@ int process_files(const char * output_dest_file) | @@ -429,22 +461,86 @@ int process_files(const char * output_dest_file) | ||
| 429 | } | 461 | } |
| 430 | } | 462 | } |
| 431 | if (nv != filesvideo.size() - 1) {// not the last one | 463 | if (nv != filesvideo.size() - 1) {// not the last one |
| 432 | - sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | ||
| 433 | - split_audio(audio, video.start_time, video.end_time - video.start_time, destfile); | ||
| 434 | - tmp_files.push_back(destfile); | 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); | ||
| 435 | 468 | ||
| 436 | - audio_start = video.end_time; | ||
| 437 | - sprintf(destfile, "%d.ts", nf); | ||
| 438 | - megre_audio_video(audio, nf, video, destfile); | ||
| 439 | - merged_files.push_back(destfile); | ||
| 440 | - nf++; | 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); | ||
| 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; | ||
| 495 | + | ||
| 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; | ||
| 500 | + | ||
| 501 | + sprintf(destfile, "%d_%d_silence.aac", nf, i); | ||
| 502 | + split_audio(silence_aac_file,0, silence_audio_end - audio.end_time, destfile); | ||
| 503 | + merge_audio_files.push_back(destfile); | ||
| 504 | + tmp_files.push_back(destfile); | ||
| 505 | + | ||
| 506 | + | ||
| 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; | ||
| 511 | + } | ||
| 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; | ||
| 518 | + } | ||
| 519 | + merge_audio_files.push_back(audio.name); | ||
| 520 | + } | ||
| 521 | + else { | ||
| 522 | + break; | ||
| 523 | + } | ||
| 524 | + } | ||
| 525 | + | ||
| 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 | + | ||
| 536 | + } | ||
| 441 | } | 537 | } |
| 442 | else { | 538 | else { |
| 443 | 539 | ||
| 444 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | 540 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 445 | 541 | ||
| 446 | if (audio.end_time - video.end_time < 1.0) { | 542 | if (audio.end_time - video.end_time < 1.0) { |
| 447 | - split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile); | 543 | + split_audio(audio.name.c_str(), video.start_time, audio.end_time - video.start_time, destfile); |
| 448 | tmp_files.push_back(destfile); | 544 | tmp_files.push_back(destfile); |
| 449 | 545 | ||
| 450 | audio_start = video.end_time; | 546 | audio_start = video.end_time; |
| @@ -455,7 +551,7 @@ int process_files(const char * output_dest_file) | @@ -455,7 +551,7 @@ int process_files(const char * output_dest_file) | ||
| 455 | nf++; | 551 | nf++; |
| 456 | } | 552 | } |
| 457 | else{ | 553 | else{ |
| 458 | - split_audio(audio, video.start_time, video.end_time - video.start_time, destfile); | 554 | + split_audio(audio.name.c_str(), video.start_time, video.end_time - video.start_time, destfile); |
| 459 | tmp_files.push_back(destfile); | 555 | tmp_files.push_back(destfile); |
| 460 | 556 | ||
| 461 | audio_start = video.end_time; | 557 | audio_start = video.end_time; |
| @@ -466,7 +562,7 @@ int process_files(const char * output_dest_file) | @@ -466,7 +562,7 @@ int process_files(const char * output_dest_file) | ||
| 466 | nf++; | 562 | nf++; |
| 467 | 563 | ||
| 468 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | 564 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 469 | - split_audio(audio, video.end_time, audio.end_time - video.end_time, destfile); | 565 | + split_audio(audio.name.c_str(), video.end_time, audio.end_time - video.end_time, destfile); |
| 470 | tmp_files.push_back(destfile); | 566 | tmp_files.push_back(destfile); |
| 471 | 567 | ||
| 472 | sprintf(destfile, "%d.ts", nf); | 568 | sprintf(destfile, "%d.ts", nf); |
-
请 注册 或 登录 后发表评论