正在显示
1 个修改的文件
包含
98 行增加
和
50 行删除
| @@ -5,26 +5,50 @@ | @@ -5,26 +5,50 @@ | ||
| 5 | #include <fstream> | 5 | #include <fstream> |
| 6 | #include <vector> | 6 | #include <vector> |
| 7 | #include <string.h> | 7 | #include <string.h> |
| 8 | +#include <list> | ||
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | bool only_print = false; | 11 | bool only_print = false; |
| 11 | bool keep_tmp_files = false; | 12 | bool keep_tmp_files = false; |
| 12 | using namespace std; | 13 | using namespace std; |
| 13 | 14 | ||
| 15 | +enum media_type{ | ||
| 16 | + mt_audio = 0, | ||
| 17 | + mt_video = 1, | ||
| 18 | +}; | ||
| 19 | + | ||
| 20 | +enum timestamp_type{ | ||
| 21 | + tt_start = 0, | ||
| 22 | + tt_end = 1; | ||
| 23 | +}; | ||
| 24 | + | ||
| 14 | class fileinfo { | 25 | class fileinfo { |
| 15 | public: | 26 | public: |
| 16 | float start_time; | 27 | float start_time; |
| 17 | float end_time; | 28 | float end_time; |
| 18 | - string start_time_str; | ||
| 19 | - string end_time_str; | ||
| 20 | string name; | 29 | string name; |
| 30 | + int index; | ||
| 31 | + media_type m_type; | ||
| 21 | }; | 32 | }; |
| 22 | 33 | ||
| 23 | -enum media_type{ | ||
| 24 | - type_audio = 0, | ||
| 25 | - type_video = 1, | 34 | +class media_info { |
| 35 | +public: | ||
| 36 | + float type_time;//the time for start or end according to the m_type | ||
| 37 | + float start_time; | ||
| 38 | + float end_time; | ||
| 39 | + string name; | ||
| 40 | + | ||
| 41 | + float duration; | ||
| 42 | + float cur_start; | ||
| 43 | + float cur_end; | ||
| 44 | + int index; | ||
| 45 | + media_type m_type; | ||
| 46 | + timestamp_type t_type; | ||
| 26 | }; | 47 | }; |
| 27 | -vector<fileinfo> media_files[2]; | 48 | + |
| 49 | + | ||
| 50 | +vector<fileinfo> media_files; | ||
| 51 | + | ||
| 28 | 52 | ||
| 29 | void run_shell_cmd(const char * cmd) | 53 | void run_shell_cmd(const char * cmd) |
| 30 | { | 54 | { |
| @@ -82,14 +106,13 @@ bool first_time_set = false; | @@ -82,14 +106,13 @@ bool first_time_set = false; | ||
| 82 | float start_time = 0.0f; | 106 | float start_time = 0.0f; |
| 83 | 107 | ||
| 84 | void addinfo(string t, string name, bool bstart){ | 108 | void addinfo(string t, string name, bool bstart){ |
| 85 | - media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? type_audio : type_video; | 109 | + media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? mt_audio : mt_video; |
| 86 | if (bstart) { | 110 | if (bstart) { |
| 87 | fileinfo f; | 111 | fileinfo f; |
| 88 | f.start_time = atof(t.c_str()); | 112 | f.start_time = atof(t.c_str()); |
| 89 | f.end_time = f.start_time; | 113 | f.end_time = f.start_time; |
| 90 | - f.start_time_str = t; | ||
| 91 | - f.end_time_str = t; | ||
| 92 | f.name = name; | 114 | f.name = name; |
| 115 | + f.m_type = mtype; | ||
| 93 | 116 | ||
| 94 | if (!first_time_set) { | 117 | if (!first_time_set) { |
| 95 | first_time_set = true; | 118 | first_time_set = true; |
| @@ -98,23 +121,21 @@ void addinfo(string t, string name, bool bstart){ | @@ -98,23 +121,21 @@ void addinfo(string t, string name, bool bstart){ | ||
| 98 | 121 | ||
| 99 | f.start_time -= start_time; | 122 | f.start_time -= start_time; |
| 100 | 123 | ||
| 101 | - media_files[mtype].push_back(f); | 124 | + media_files.push_back(f); |
| 102 | } | 125 | } |
| 103 | else { | 126 | else { |
| 104 | - vector<fileinfo> & files = media_files[mtype]; | ||
| 105 | int i; | 127 | int i; |
| 106 | - for (i = 0; i < files.size(); i++) { | ||
| 107 | - if (files[i].name == name) { | ||
| 108 | - files[i].end_time = atof(t.c_str()); | ||
| 109 | - files[i].end_time_str = t; | ||
| 110 | - files[i].end_time -= start_time; | 128 | + for (i = 0; i < media_files.size(); i++) { |
| 129 | + if (media_files[i].name == name) { | ||
| 130 | + media_files[i].end_time = atof(t.c_str()); | ||
| 131 | + media_files[i].end_time -= start_time; | ||
| 111 | break; | 132 | break; |
| 112 | } | 133 | } |
| 113 | } | 134 | } |
| 114 | 135 | ||
| 115 | - if (i == files.size()) | 136 | + if (i == media_files.size()) |
| 116 | { | 137 | { |
| 117 | - cout << "\nerror ,file : " << name << " close but not started!"; | 138 | + printf("\nerror ,file : %s close but not started!", name.c_str()); |
| 118 | } | 139 | } |
| 119 | } | 140 | } |
| 120 | } | 141 | } |
| @@ -336,18 +357,16 @@ float get_file_duration(const char *mediafile, bool bVideo) | @@ -336,18 +357,16 @@ float get_file_duration(const char *mediafile, bool bVideo) | ||
| 336 | 357 | ||
| 337 | void get_duration_from_video_file() | 358 | void get_duration_from_video_file() |
| 338 | { | 359 | { |
| 339 | - vector<fileinfo> & filesvideo = media_files[type_video]; | ||
| 340 | - vector<string> tmp_files; | ||
| 341 | - | ||
| 342 | bool tmp = only_print; | 360 | bool tmp = only_print; |
| 343 | only_print = false; | 361 | only_print = false; |
| 344 | 362 | ||
| 345 | - | ||
| 346 | - for (int i = 0; i < filesvideo.size(); i++){ | ||
| 347 | - float duration = get_file_duration(filesvideo[i].name.c_str(), true); | ||
| 348 | - if (duration >= 0.10) { | ||
| 349 | - printf("file:%s , duration in recording file: %.3f, duration parsed from file: %.3f\n", filesvideo[i].name.c_str(), filesvideo[i].end_time - filesvideo[i].start_time, duration); | ||
| 350 | - filesvideo[i].end_time = filesvideo[i].start_time + duration; | 363 | + for (int i = 0; i < media_files.size(); i++){ |
| 364 | + if (media_files[i].m_type == mt_video) { | ||
| 365 | + float duration = get_file_duration(media_files[i].name.c_str(), true); | ||
| 366 | + if (duration >= 0.1) { | ||
| 367 | + printf("file:%s , duration in recording file: %.3f, duration parsed from file: %.3f\n", media_files[i].name.c_str(), media_files[i].end_time - media_files[i].start_time, duration); | ||
| 368 | + media_files[i].end_time = media_files[i].start_time + duration; | ||
| 369 | + } | ||
| 351 | } | 370 | } |
| 352 | } | 371 | } |
| 353 | 372 | ||
| @@ -357,19 +376,17 @@ void get_duration_from_video_file() | @@ -357,19 +376,17 @@ void get_duration_from_video_file() | ||
| 357 | 376 | ||
| 358 | void check_audio_duration() | 377 | void check_audio_duration() |
| 359 | { | 378 | { |
| 360 | - vector<fileinfo> & filesaudio = media_files[type_audio]; | ||
| 361 | - vector<string> tmp_files; | ||
| 362 | - | ||
| 363 | bool tmp = only_print; | 379 | bool tmp = only_print; |
| 364 | only_print = false; | 380 | only_print = false; |
| 365 | 381 | ||
| 366 | - | ||
| 367 | - for (int i = 0; i < filesaudio.size(); i++){ | ||
| 368 | - fileinfo f = filesaudio[i]; | ||
| 369 | - if (f.end_time - f.start_time < 0.1){ | ||
| 370 | - float duration = get_file_duration(f.name.c_str(), false); | ||
| 371 | - filesaudio[i].end_time = f.start_time + duration; | ||
| 372 | - printf("file:%s , duration in recording file maybe is not set, duration parsed from file: %.3f\n",f.name.c_str(), duration); | 382 | + for (int i = 0; i < media_files.size(); i++){ |
| 383 | + if (media_files[i].m_type == mt_audio) { | ||
| 384 | + fileinfo f = media_files[i]; | ||
| 385 | + if (f.end_time - f.start_time < 0.1){//avoid no close | ||
| 386 | + float duration = get_file_duration(f.name.c_str(), false); | ||
| 387 | + media_files[i].end_time = f.start_time + duration; | ||
| 388 | + printf("file:%s , duration in recording file maybe is not set, duration parsed from file: %.3f\n", f.name.c_str(), duration); | ||
| 389 | + } | ||
| 373 | } | 390 | } |
| 374 | } | 391 | } |
| 375 | 392 | ||
| @@ -404,22 +421,22 @@ int merge_audio_file(vector<string> & files, const char * dest) | @@ -404,22 +421,22 @@ int merge_audio_file(vector<string> & files, const char * dest) | ||
| 404 | 421 | ||
| 405 | class MergeProcess{ | 422 | class MergeProcess{ |
| 406 | public: | 423 | public: |
| 407 | - MergeProcess(vector<fileinfo> & a, vector<fileinfo> & v); | 424 | + MergeProcess(); |
| 408 | int process_files(const char * output_dest_file); | 425 | int process_files(const char * output_dest_file); |
| 409 | 426 | ||
| 410 | protected: | 427 | 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(); | 428 | + void add_media_infos(); |
| 429 | + void init(); | ||
| 430 | + void adjust_va_timestamp(); | ||
| 431 | + void merge_left_audio(); | ||
| 432 | + int process_video_ahead_of_audio(); | ||
| 433 | + int process_video_behind_audio(); | ||
| 434 | + int process_video_align_audio(); | ||
| 435 | + int process_video_loop(); | ||
| 436 | + int process_va(); | ||
| 419 | 437 | ||
| 420 | protected: | 438 | protected: |
| 421 | - vector<fileinfo> & filesaudio; | ||
| 422 | - vector<fileinfo> & filesvideo; | 439 | + vector<media_info> media_infos; |
| 423 | vector<string > merged_files; | 440 | vector<string > merged_files; |
| 424 | vector<string> tmp_files; | 441 | vector<string> tmp_files; |
| 425 | int nv; // the index of processing video file | 442 | int nv; // the index of processing video file |
| @@ -431,13 +448,42 @@ protected: | @@ -431,13 +448,42 @@ protected: | ||
| 431 | int audio_start; | 448 | int audio_start; |
| 432 | fileinfo audio; | 449 | fileinfo audio; |
| 433 | fileinfo video; | 450 | fileinfo video; |
| 451 | +private: | ||
| 452 | + | ||
| 453 | + void add_media_info(media_info m) | ||
| 454 | + { | ||
| 455 | + throw std::logic_error("The method or operation is not implemented."); | ||
| 456 | + } | ||
| 457 | + | ||
| 458 | + | ||
| 434 | }; | 459 | }; |
| 435 | 460 | ||
| 436 | -MergeProcess::MergeProcess(vector<fileinfo> & a, vector<fileinfo> & v) :filesaudio(a), filesvideo(v) | ||
| 437 | -{ | 461 | +MergeProcess::MergeProcess(){ |
| 438 | init(); | 462 | init(); |
| 439 | } | 463 | } |
| 440 | 464 | ||
| 465 | +void MergeProcess::add_media_infos() | ||
| 466 | +{ | ||
| 467 | + for (int i = 0; i < media_files.size(); i++) { | ||
| 468 | + fileinfo f = media_files[i]; | ||
| 469 | + media_info m; | ||
| 470 | + m.index = i; | ||
| 471 | + m.name = f.name; | ||
| 472 | + m.start_time = f.start_time; | ||
| 473 | + m.end_time = f.end_time; | ||
| 474 | + m.m_type = f.m_type; | ||
| 475 | + m.t_type = tt_start; | ||
| 476 | + m.duration = f.end_time - f.start_time; | ||
| 477 | + m.cur_start = 0; | ||
| 478 | + m.cur_end = m.duration; | ||
| 479 | + m.type_time = m.start_time; | ||
| 480 | + add_media_info(m); | ||
| 481 | + m.t_type = tt_end; | ||
| 482 | + m.type_time = m.end_time; | ||
| 483 | + add_media_info(m); | ||
| 484 | + } | ||
| 485 | +} | ||
| 486 | + | ||
| 441 | void MergeProcess::init() | 487 | void MergeProcess::init() |
| 442 | { | 488 | { |
| 443 | strcpy(blank_pic_file, cfg_path); | 489 | strcpy(blank_pic_file, cfg_path); |
| @@ -448,6 +494,8 @@ void MergeProcess::init() | @@ -448,6 +494,8 @@ void MergeProcess::init() | ||
| 448 | 494 | ||
| 449 | check_audio_duration(); | 495 | check_audio_duration(); |
| 450 | get_duration_from_video_file(); | 496 | get_duration_from_video_file(); |
| 497 | + add_media_infos(); | ||
| 498 | + | ||
| 451 | nv = 0; | 499 | nv = 0; |
| 452 | nf = 0; | 500 | nf = 0; |
| 453 | audio_index = 0; | 501 | audio_index = 0; |
-
请 注册 或 登录 后发表评论