胡斌

1.convert webm to mkv to parsing the duration

2.using the length parsed from video file,not the length in recording info file
3.append blank video to audio of the last segment if the duration is big than 1s
... ... @@ -28,10 +28,9 @@ vector<fileinfo> media_files[2];
void run_shell_cmd(const char * cmd)
{
if (only_print){
printf("%s\n", cmd);
}
else {
printf("run command:%s\n", cmd);
if (!only_print){
system(cmd);
}
}
... ... @@ -154,6 +153,13 @@ void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfi
run_shell_cmd(buf);
}
void merge_audio_pic(fileinfo audio, int nf, const char * picfile, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %d_%s -loop 0 -shortest %s %s %s", picfile, nf, audio.name.c_str(), acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile)
{
char buf[2048];
... ... @@ -203,15 +209,117 @@ void adjust_dest_timecode(const char * src, const char * dest)
#include <unistd.h>
#endif
void remove_file(const char * file)
{
#ifdef WIN32
_unlink(file);
#else
unlink(file);
#endif
}
void removefiles(vector<string> files)
{
for (int i = 0; i < files.size(); i++) {
remove_file(files[i].c_str());
}
}
float get_video_duration(const char *videofile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -vcodec copy -an %s.mkv", videofile, videofile);
run_shell_cmd(buf);
#ifdef WIN32
_unlink(files[i].c_str());
sprintf(buf, "ffmpeg -i %s.mkv > %s.txt 2>&1", videofile, videofile);
#else
unlink(files[i].c_str());
sprintf(buf, "ffmpeg -i %s.mkv &> %s.txt", videofile, videofile);
#endif
run_shell_cmd(buf);
if (!keep_tmp_files) {
char buf[2048];
sprintf(buf, "%s.mkv", videofile);
remove_file(buf);
}
sprintf(buf, "%s.txt", videofile);
FILE * fp = fopen(buf, "rb");
if (!fp) {
printf("\nOpen %s error\n", buf);
return -1.0;
}
fseek(fp, 0l, SEEK_END);
long file_len = ftell(fp);
fseek(fp, 0l, SEEK_SET);
char * content = new char[file_len + 1];
fread(content, 1, file_len, fp);
fclose(fp);
content[file_len] = 0;
if (!keep_tmp_files) {
char buf[2048];
sprintf(buf, "%s.txt", videofile);
remove_file(buf);
}
char * pDuration = strstr(content, "Duration:");
if (!pDuration){
printf("\ncan't find 'Duration:' in %s\n", buf);
delete content;
return -1.0;
}
char * pComma = strstr(pDuration, ",");
if (!pDuration){
printf("\ncan't find ',' after 'Duration:' in %s\n", buf);
delete content;
return -1.0;
}
int len = pComma - pDuration - 10;
strncpy(content, pDuration + 10, len);
content[len] = 0;
vector <string > hms;
split(content, ":", hms);
delete content;
if (hms.size() != 3) {
printf("\nerro parsing duration in %s, the duration string is:%s\n", buf, content);
delete content;
return -1.0;
}
int hour = atoi(hms[0].c_str());
int minute = atoi(hms[1].c_str());
float sec = atof(hms[2].c_str());
return hour * 3600 + minute * 60 + sec;
}
void get_duration_from_video_file()
{
vector<fileinfo> & filesvideo = media_files[type_video];
vector<string> tmp_files;
bool tmp = only_print;
only_print = false;
for (int i = 0; i < filesvideo.size(); i++){
float duration = get_video_duration(filesvideo[i].name.c_str());
if (duration >= 0.10) {
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);
filesvideo[i].end_time = filesvideo[i].start_time + duration;
}
}
only_print = tmp;
}
int process_files(const char * output_dest_file)
... ... @@ -223,8 +331,12 @@ int process_files(const char * output_dest_file)
int nv = 0;
int nf = 0;
char destfile[1024];
char blank_pic_file[1024];
get_config_path();
strcpy(blank_pic_file, cfg_path);
strcat(blank_pic_file, "blank.jpg");
get_duration_from_video_file();
if (filesvideo.size()) {//has video files
for (int i = 0; i < filesaudio.size(); i++){ //
... ... @@ -263,25 +375,45 @@ int process_files(const char * output_dest_file)
nf++;
}
else {
sprintf(destfile, "%d_%s", nf, audio.name.c_str());
split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile);
tmp_files.push_back(destfile);
audio_start = video.end_time;
sprintf(destfile, "%d.ts", nf);
megre_audio_video(audio, nf, video, destfile);
merged_files.push_back(destfile);
nf++;
if (audio.end_time - video.end_time < 1.0) {
split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile);
tmp_files.push_back(destfile);
audio_start = video.end_time;
sprintf(destfile, "%d.ts", nf);
megre_audio_video(audio, nf, video, destfile);
merged_files.push_back(destfile);
nf++;
}
else{
split_audio(audio, video.start_time, video.end_time - video.start_time, destfile);
tmp_files.push_back(destfile);
audio_start = video.end_time;
sprintf(destfile, "%d.ts", nf);
megre_audio_video(audio, nf, video, destfile);
merged_files.push_back(destfile);
nf++;
sprintf(destfile, "%d_%s", nf, audio.name.c_str());
split_audio(audio, video.end_time, audio.end_time - video.end_time, destfile);
tmp_files.push_back(destfile);
sprintf(destfile, "%d.ts", nf);
merge_audio_pic(audio, nf, blank_pic_file, destfile);
merged_files.push_back(destfile);
nf++;
}
}
}
}
}
else {//only audio
char blank_pic_file[1024];
strcpy(blank_pic_file, cfg_path);
strcat(blank_pic_file, "blank.jpg");
if (filesaudio.size() == 1){
fileinfo audio = filesaudio[0];
... ... @@ -393,6 +525,8 @@ int main(int argc, char * argv[])
}
}
get_config_path();
load_codec_param();
process_files("dest.ts");
... ...