胡斌

support keep or delete the temp files,using -k option

... ... @@ -8,6 +8,7 @@
bool only_print = false;
bool keep_tmp_files = false;
using namespace std;
class fileinfo {
... ... @@ -35,8 +36,8 @@ void run_shell_cmd(const char * cmd)
}
}
const char * default_vcodec_param = " -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 ";
const char * default_acodec_param = " -acodec copy ";
const char * default_vcodec_param = "-vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20";
const char * default_acodec_param = "-acodec copy";
char vcodec_param[1024];
char acodec_param[1024];
... ... @@ -87,17 +88,17 @@ void split(string str, string separator, vector<string> &result, bool includeEmp
result.push_back(str.substr(lastPosition, string::npos));
}
void split_audio(fileinfo audio, float audio_start, float duration, int nf)
void split_audio(fileinfo audio, float audio_start, float duration, char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -ss %.3f -t %.3f %s %d_%s", audio.name.c_str(), audio_start, duration, acodec_param, nf, audio.name.c_str());
sprintf(buf, "ffmpeg -y -i %s -ss %.3f -t %.3f %s %s", audio.name.c_str(), audio_start, duration, acodec_param, destfile);
run_shell_cmd(buf);
}
void get_video_firstframe(fileinfo video)
void get_video_first_frame_jpeg(fileinfo video, char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -vframes 1 -ss 0 -f mjpeg -an %s.jpg", video.name.c_str(), video.name.c_str());
sprintf(buf, "ffmpeg -y -i %s -vframes 1 -ss 0 -f mjpeg -an %s", video.name.c_str(), destfile);
run_shell_cmd(buf);
}
... ... @@ -115,7 +116,7 @@ void megre_audio_video(fileinfo audio, int nf, fileinfo video, char * destfile)
run_shell_cmd(buf);
}
void concate_files(vector<string > merged_files)
void concate_files(vector<string > merged_files, char * destfile)
{
char buf[2048];
#ifdef WIN32
... ... @@ -125,6 +126,7 @@ void concate_files(vector<string > merged_files)
strcat(buf, " + /B ");
strcat(buf, merged_files[i].c_str());
}
strcat(buf, " ");
#else
strcpy(buf, "cat ");
... ... @@ -132,41 +134,64 @@ void concate_files(vector<string > merged_files)
strcat(buf, " ");
strcat(buf, merged_files[i].c_str());
}
strcat(buf, ">");
strcat(buf, "> ");
#endif // WIN32
strcat(buf, " m.ts");
strcat(buf, destfile);
run_shell_cmd(buf);
}
void adjust_dest_timecode()
void adjust_dest_timecode(char * src , char * dest)
{
run_shell_cmd("ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts");
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -acodec copy -vcodec copy %s", src, dest);
run_shell_cmd(buf);
}
void removefiles(vector<string> files)
{
for (int i = 0; i < files.size(); i++) {
_unlink(files[i].c_str());
}
}
int process_files()
int process_files(char * output_dest_file)
{
vector<fileinfo> & filesaudio = media_files[type_audio];
vector<fileinfo> & filesvideo = media_files[type_video];
vector<string > merged_files;
vector<string> tmp_files;
int nv = 0;
int nf = 0;
char destfile[1024];
for (int i = 0; i < filesaudio.size(); i++){ //
fileinfo audio = filesaudio[i];
float audio_start = 0;
for (; nv < filesvideo.size(); nv++) {
fileinfo video = filesvideo[nv];
if (video.start_time - audio_start > 0.100) {
split_audio(audio, audio_start, video.start_time - audio_start, nf);
sprintf( destfile, "%d_%s", nf, audio.name.c_str() );
split_audio(audio, audio_start, video.start_time - audio_start, destfile);
tmp_files.push_back(destfile);
audio_start = video.start_time;
get_video_firstframe(video);
sprintf(destfile, "%s.jpg", video.name.c_str());
get_video_first_frame_jpeg(video, destfile);
tmp_files.push_back(destfile);
sprintf(destfile, "%d.ts", nf);
merge_audio_pic(audio, nf, video, destfile);
merged_files.push_back(destfile);
nf++;
}
if (nv != filesvideo.size() - 1) {// not the last one
split_audio(audio, video.start_time, video.end_time - video.start_time, nf);
sprintf(destfile, "%d_%s", nf, audio.name.c_str());
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);
... ... @@ -175,8 +200,12 @@ int process_files()
nf++;
}
else {
split_audio(audio, video.start_time, audio.end_time - video.start_time, nf);
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);
... ... @@ -185,8 +214,16 @@ int process_files()
}
}
concate_files(merged_files);
adjust_dest_timecode();
concate_files(merged_files , "m.ts");
tmp_files.push_back("m.ts");
adjust_dest_timecode("m.ts", output_dest_file);
if (!keep_tmp_files) {
removefiles(tmp_files);
removefiles(merged_files);
}
return 0;
}
... ... @@ -276,8 +313,9 @@ int main(int argc, char * argv[])
if (argc < 2) {
printf(" merge_av 1.0.0\n");
printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:");
printf("\n %s record_info_filename [-p]", argv[0]);
printf("\n %s record_info_filename [-p] [-k]", argv[0]);
printf("\n -p :only print the command,don't run ffmpeg\n");
printf("\n -k :keep the temp files\n");
return -1;
}
... ... @@ -286,14 +324,18 @@ int main(int argc, char * argv[])
return -2;
}
if (argc == 3 && !strcmp(argv[2], "-p")) {
only_print = true;
for (int i = 2; i < argc; i++){
if (!strcmp(argv[i], "-p")){
only_print = true;
}
else if (!strcmp(argv[i], "-k")){
keep_tmp_files = true;
}
}
load_codec_param();
process_files();
process_files("dest.ts");
return 0;
}
... ...