胡斌

for only convert mp4,only get the codec,don't get the true duration

@@ -337,6 +337,24 @@ float parse_ffmpeg_duration(const char * file, bool * beH264 = NULL) @@ -337,6 +337,24 @@ float parse_ffmpeg_duration(const char * file, bool * beH264 = NULL)
337 return hour * 3600 + minute * 60 + sec; 337 return hour * 3600 + minute * 60 + sec;
338 } 338 }
339 339
  340 +float get_mp4_duration_from_header(const char *mediafile, bool * bH264)
  341 +{
  342 + char buf[2048];
  343 +
  344 +#ifdef WIN32
  345 + sprintf(buf, "ffmpeg -i %s > %s.txt 2>&1", mediafile, mediafile);
  346 +#else
  347 + sprintf(buf, "ffmpeg -i %s &> %s.txt", mediafile, mediafile);
  348 +#endif
  349 +
  350 + run_shell_cmd(buf);
  351 +
  352 +
  353 + sprintf(buf, "%s.txt", mediafile);
  354 + return parse_ffmpeg_duration(buf, bH264);
  355 +}
  356 +
  357 +//because the duration in mp4 header is wrong in some file,so convert it to ts and to mkv to get the right duration
340 float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264) 358 float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264)
341 { 359 {
342 char buf[2048]; 360 char buf[2048];
@@ -382,6 +400,40 @@ float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264) @@ -382,6 +400,40 @@ float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264)
382 #define strcasecmp _stricmp 400 #define strcasecmp _stricmp
383 #endif 401 #endif
384 402
  403 +float get_file_codec_h264(const char *mediafile, bool * beH264 = NULL)
  404 +{
  405 + char buf[2048];
  406 + int len = strlen(mediafile);
  407 + if (len > 3) {
  408 + if (!strcasecmp(mediafile + len - 4, ".mp4")) {
  409 + return get_mp4_duration_from_header(mediafile, beH264);
  410 + }
  411 + }
  412 +
  413 + sprintf(buf, "ffmpeg -y -i %s -acodec copy -vcodec copy %s.mkv", mediafile, mediafile);
  414 +
  415 +
  416 + run_shell_cmd(buf);
  417 +#ifdef WIN32
  418 + sprintf(buf, "ffmpeg -i %s.mkv > %s.txt 2>&1", mediafile, mediafile);
  419 +#else
  420 + sprintf(buf, "ffmpeg -i %s.mkv &> %s.txt", mediafile, mediafile);
  421 +#endif
  422 +
  423 + run_shell_cmd(buf);
  424 +
  425 + if (!keep_tmp_files) {
  426 + char buf[2048];
  427 + sprintf(buf, "%s.mkv", mediafile);
  428 + remove_file(buf);
  429 + }
  430 +
  431 +
  432 + sprintf(buf, "%s.txt", mediafile);
  433 + float duration = parse_ffmpeg_duration(buf, beH264);
  434 + return duration;
  435 +}
  436 +
385 float get_file_duration(const char *mediafile, bool bVideo, bool * beH264 = NULL) 437 float get_file_duration(const char *mediafile, bool bVideo, bool * beH264 = NULL)
386 { 438 {
387 char buf[2048]; 439 char buf[2048];
@@ -438,6 +490,23 @@ void get_duration_from_video_file() @@ -438,6 +490,23 @@ void get_duration_from_video_file()
438 only_print = tmp; 490 only_print = tmp;
439 } 491 }
440 492
  493 +void get_video_codec_h264()
  494 +{
  495 + bool tmp = only_print;
  496 + only_print = false;
  497 +
  498 + for (int i = 0; i < media_files.size(); i++){
  499 + if (media_files[i].m_type == mt_video) {
  500 + bool bH264 = false;
  501 +
  502 + get_file_codec_h264(media_files[i].name.c_str(),&bH264);
  503 +
  504 + media_files[i].is_h264 = bH264;
  505 + }
  506 + }
  507 +
  508 + only_print = tmp;
  509 +}
441 510
442 void check_audio_duration() 511 void check_audio_duration()
443 { 512 {
@@ -546,8 +615,13 @@ void init() @@ -546,8 +615,13 @@ void init()
546 strcpy(silence_aac_file, cfg_path); 615 strcpy(silence_aac_file, cfg_path);
547 strcat(silence_aac_file, "silence.aac"); 616 strcat(silence_aac_file, "silence.aac");
548 617
549 - check_audio_duration();  
550 - get_duration_from_video_file(); 618 + if (only_convert_mp4) {
  619 + get_video_codec_h264();
  620 + }
  621 + else {
  622 + check_audio_duration();
  623 + get_duration_from_video_file();
  624 + }
551 625
552 add_media_infos(); 626 add_media_infos();
553 627