正在显示
1 个修改的文件
包含
61 行增加
和
30 行删除
| @@ -233,30 +233,11 @@ void removefiles(vector<string> files) | @@ -233,30 +233,11 @@ void removefiles(vector<string> files) | ||
| 233 | } | 233 | } |
| 234 | } | 234 | } |
| 235 | 235 | ||
| 236 | -float get_video_duration(const char *videofile) | 236 | +float parse_ffmpeg_duration(const char * file) |
| 237 | { | 237 | { |
| 238 | - char buf[2048]; | ||
| 239 | - sprintf(buf, "ffmpeg -y -i %s -vcodec copy -an %s.mkv", videofile, videofile); | ||
| 240 | - run_shell_cmd(buf); | ||
| 241 | -#ifdef WIN32 | ||
| 242 | - sprintf(buf, "ffmpeg -i %s.mkv > %s.txt 2>&1", videofile, videofile); | ||
| 243 | -#else | ||
| 244 | - sprintf(buf, "ffmpeg -i %s.mkv &> %s.txt", videofile, videofile); | ||
| 245 | -#endif | ||
| 246 | - | ||
| 247 | - run_shell_cmd(buf); | ||
| 248 | - | ||
| 249 | - if (!keep_tmp_files) { | ||
| 250 | - char buf[2048]; | ||
| 251 | - sprintf(buf, "%s.mkv", videofile); | ||
| 252 | - remove_file(buf); | ||
| 253 | - } | ||
| 254 | - | ||
| 255 | - sprintf(buf, "%s.txt", videofile); | ||
| 256 | - | ||
| 257 | - FILE * fp = fopen(buf, "rb"); | 238 | + FILE * fp = fopen(file, "rb"); |
| 258 | if (!fp) { | 239 | if (!fp) { |
| 259 | - printf("\nOpen %s error\n", buf); | 240 | + printf("\nOpen %s error\n", file); |
| 260 | return -1.0; | 241 | return -1.0; |
| 261 | } | 242 | } |
| 262 | fseek(fp, 0l, SEEK_END); | 243 | fseek(fp, 0l, SEEK_END); |
| @@ -270,21 +251,19 @@ float get_video_duration(const char *videofile) | @@ -270,21 +251,19 @@ float get_video_duration(const char *videofile) | ||
| 270 | content[file_len] = 0; | 251 | content[file_len] = 0; |
| 271 | 252 | ||
| 272 | if (!keep_tmp_files) { | 253 | if (!keep_tmp_files) { |
| 273 | - char buf[2048]; | ||
| 274 | - sprintf(buf, "%s.txt", videofile); | ||
| 275 | - remove_file(buf); | 254 | + remove_file(file); |
| 276 | } | 255 | } |
| 277 | 256 | ||
| 278 | char * pDuration = strstr(content, "Duration:"); | 257 | char * pDuration = strstr(content, "Duration:"); |
| 279 | if (!pDuration){ | 258 | if (!pDuration){ |
| 280 | - printf("\ncan't find 'Duration:' in %s\n", buf); | 259 | + printf("\ncan't find 'Duration:' in %s\n", file); |
| 281 | delete content; | 260 | delete content; |
| 282 | return -1.0; | 261 | return -1.0; |
| 283 | } | 262 | } |
| 284 | 263 | ||
| 285 | char * pComma = strstr(pDuration, ","); | 264 | char * pComma = strstr(pDuration, ","); |
| 286 | if (!pDuration){ | 265 | if (!pDuration){ |
| 287 | - printf("\ncan't find ',' after 'Duration:' in %s\n", buf); | 266 | + printf("\ncan't find ',' after 'Duration:' in %s\n", file); |
| 288 | delete content; | 267 | delete content; |
| 289 | return -1.0; | 268 | return -1.0; |
| 290 | } | 269 | } |
| @@ -297,7 +276,7 @@ float get_video_duration(const char *videofile) | @@ -297,7 +276,7 @@ float get_video_duration(const char *videofile) | ||
| 297 | delete content; | 276 | delete content; |
| 298 | 277 | ||
| 299 | if (hms.size() != 3) { | 278 | if (hms.size() != 3) { |
| 300 | - printf("\nerro parsing duration in %s, the duration string is:%s\n", buf, content); | 279 | + printf("\nerro parsing duration in %s, the duration string is:%s\n", file, content); |
| 301 | delete content; | 280 | delete content; |
| 302 | return -1.0; | 281 | return -1.0; |
| 303 | } | 282 | } |
| @@ -309,6 +288,35 @@ float get_video_duration(const char *videofile) | @@ -309,6 +288,35 @@ float get_video_duration(const char *videofile) | ||
| 309 | return hour * 3600 + minute * 60 + sec; | 288 | return hour * 3600 + minute * 60 + sec; |
| 310 | } | 289 | } |
| 311 | 290 | ||
| 291 | +float get_file_duration(const char *mediafile, bool bVideo) | ||
| 292 | +{ | ||
| 293 | + char buf[2048]; | ||
| 294 | + if (bVideo){ | ||
| 295 | + sprintf(buf, "ffmpeg -y -i %s -vcodec copy -an %s.mkv", mediafile, mediafile); | ||
| 296 | + } | ||
| 297 | + else { | ||
| 298 | + sprintf(buf, "ffmpeg -y -i %s -acodec copy -vn %s.mkv", mediafile, mediafile); | ||
| 299 | + } | ||
| 300 | + run_shell_cmd(buf); | ||
| 301 | +#ifdef WIN32 | ||
| 302 | + sprintf(buf, "ffmpeg -i %s.mkv > %s.txt 2>&1", mediafile, mediafile); | ||
| 303 | +#else | ||
| 304 | + sprintf(buf, "ffmpeg -i %s.mkv &> %s.txt", mediafile, mediafile); | ||
| 305 | +#endif | ||
| 306 | + | ||
| 307 | + run_shell_cmd(buf); | ||
| 308 | + | ||
| 309 | + if (!keep_tmp_files) { | ||
| 310 | + char buf[2048]; | ||
| 311 | + sprintf(buf, "%s.mkv", mediafile); | ||
| 312 | + remove_file(buf); | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + | ||
| 316 | + sprintf(buf, "%s.txt", mediafile); | ||
| 317 | + return parse_ffmpeg_duration(buf); | ||
| 318 | +} | ||
| 319 | + | ||
| 312 | void get_duration_from_video_file() | 320 | void get_duration_from_video_file() |
| 313 | { | 321 | { |
| 314 | vector<fileinfo> & filesvideo = media_files[type_video]; | 322 | vector<fileinfo> & filesvideo = media_files[type_video]; |
| @@ -319,7 +327,7 @@ void get_duration_from_video_file() | @@ -319,7 +327,7 @@ void get_duration_from_video_file() | ||
| 319 | 327 | ||
| 320 | 328 | ||
| 321 | for (int i = 0; i < filesvideo.size(); i++){ | 329 | for (int i = 0; i < filesvideo.size(); i++){ |
| 322 | - float duration = get_video_duration(filesvideo[i].name.c_str()); | 330 | + float duration = get_file_duration(filesvideo[i].name.c_str(), true); |
| 323 | if (duration >= 0.10) { | 331 | if (duration >= 0.10) { |
| 324 | 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); | 332 | 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); |
| 325 | filesvideo[i].end_time = filesvideo[i].start_time + duration; | 333 | filesvideo[i].end_time = filesvideo[i].start_time + duration; |
| @@ -329,6 +337,28 @@ void get_duration_from_video_file() | @@ -329,6 +337,28 @@ void get_duration_from_video_file() | ||
| 329 | only_print = tmp; | 337 | only_print = tmp; |
| 330 | } | 338 | } |
| 331 | 339 | ||
| 340 | + | ||
| 341 | +void check_audio_duration() | ||
| 342 | +{ | ||
| 343 | + vector<fileinfo> & filesaudio = media_files[type_audio]; | ||
| 344 | + vector<string> tmp_files; | ||
| 345 | + | ||
| 346 | + bool tmp = only_print; | ||
| 347 | + only_print = false; | ||
| 348 | + | ||
| 349 | + | ||
| 350 | + for (int i = 0; i < filesaudio.size(); i++){ | ||
| 351 | + fileinfo f = filesaudio[i]; | ||
| 352 | + if (f.end_time - f.start_time < 0.1){ | ||
| 353 | + float duration = get_file_duration(f.name.c_str(), false); | ||
| 354 | + filesaudio[i].end_time = f.start_time + duration; | ||
| 355 | + printf("file:%s , duration in recording file maybe is not set, duration parsed from file: %.3f\n",f.name.c_str(), duration); | ||
| 356 | + } | ||
| 357 | + } | ||
| 358 | + | ||
| 359 | + only_print = tmp; | ||
| 360 | +} | ||
| 361 | + | ||
| 332 | int process_files(const char * output_dest_file) | 362 | int process_files(const char * output_dest_file) |
| 333 | { | 363 | { |
| 334 | vector<fileinfo> & filesaudio = media_files[type_audio]; | 364 | vector<fileinfo> & filesaudio = media_files[type_audio]; |
| @@ -347,6 +377,7 @@ int process_files(const char * output_dest_file) | @@ -347,6 +377,7 @@ int process_files(const char * output_dest_file) | ||
| 347 | strcpy(silence_aac_file, cfg_path); | 377 | strcpy(silence_aac_file, cfg_path); |
| 348 | strcat(silence_aac_file, "silence.aac"); | 378 | strcat(silence_aac_file, "silence.aac"); |
| 349 | 379 | ||
| 380 | + check_audio_duration(); | ||
| 350 | get_duration_from_video_file(); | 381 | get_duration_from_video_file(); |
| 351 | 382 | ||
| 352 | if (filesvideo.size()) {//has video files | 383 | if (filesvideo.size()) {//has video files |
| @@ -553,7 +584,7 @@ void load_codec_param() | @@ -553,7 +584,7 @@ void load_codec_param() | ||
| 553 | int main(int argc, char * argv[]) | 584 | int main(int argc, char * argv[]) |
| 554 | { | 585 | { |
| 555 | if (argc < 2) { | 586 | if (argc < 2) { |
| 556 | - printf(" merge_av 1.0.4\n"); | 587 | + printf(" merge_av 1.0.5\n"); |
| 557 | printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:"); | 588 | printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:"); |
| 558 | printf("\n %s record_info_filename [-p] [-k]", argv[0]); | 589 | printf("\n %s record_info_filename [-p] [-k]", argv[0]); |
| 559 | printf("\n -p :only print the command,don't run ffmpeg"); | 590 | printf("\n -p :only print the command,don't run ffmpeg"); |
-
请 注册 或 登录 后发表评论