胡斌

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)
return hour * 3600 + minute * 60 + sec;
}
float get_mp4_duration_from_header(const char *mediafile, bool * bH264)
{
char buf[2048];
#ifdef WIN32
sprintf(buf, "ffmpeg -i %s > %s.txt 2>&1", mediafile, mediafile);
#else
sprintf(buf, "ffmpeg -i %s &> %s.txt", mediafile, mediafile);
#endif
run_shell_cmd(buf);
sprintf(buf, "%s.txt", mediafile);
return parse_ffmpeg_duration(buf, bH264);
}
//because the duration in mp4 header is wrong in some file,so convert it to ts and to mkv to get the right duration
float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264)
{
char buf[2048];
... ... @@ -382,6 +400,40 @@ float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264)
#define strcasecmp _stricmp
#endif
float get_file_codec_h264(const char *mediafile, bool * beH264 = NULL)
{
char buf[2048];
int len = strlen(mediafile);
if (len > 3) {
if (!strcasecmp(mediafile + len - 4, ".mp4")) {
return get_mp4_duration_from_header(mediafile, beH264);
}
}
sprintf(buf, "ffmpeg -y -i %s -acodec copy -vcodec copy %s.mkv", mediafile, mediafile);
run_shell_cmd(buf);
#ifdef WIN32
sprintf(buf, "ffmpeg -i %s.mkv > %s.txt 2>&1", mediafile, mediafile);
#else
sprintf(buf, "ffmpeg -i %s.mkv &> %s.txt", mediafile, mediafile);
#endif
run_shell_cmd(buf);
if (!keep_tmp_files) {
char buf[2048];
sprintf(buf, "%s.mkv", mediafile);
remove_file(buf);
}
sprintf(buf, "%s.txt", mediafile);
float duration = parse_ffmpeg_duration(buf, beH264);
return duration;
}
float get_file_duration(const char *mediafile, bool bVideo, bool * beH264 = NULL)
{
char buf[2048];
... ... @@ -438,6 +490,23 @@ void get_duration_from_video_file()
only_print = tmp;
}
void get_video_codec_h264()
{
bool tmp = only_print;
only_print = false;
for (int i = 0; i < media_files.size(); i++){
if (media_files[i].m_type == mt_video) {
bool bH264 = false;
get_file_codec_h264(media_files[i].name.c_str(),&bH264);
media_files[i].is_h264 = bH264;
}
}
only_print = tmp;
}
void check_audio_duration()
{
... ... @@ -546,8 +615,13 @@ void init()
strcpy(silence_aac_file, cfg_path);
strcat(silence_aac_file, "silence.aac");
if (only_convert_mp4) {
get_video_codec_h264();
}
else {
check_audio_duration();
get_duration_from_video_file();
}
add_media_infos();
... ...