胡斌

for the record file only have audio,merge with blank.jpg

... ... @@ -50,7 +50,23 @@ ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts
调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20
音频编码参数是: -acodec copy
如果要修改,windows下在merge_av.exe同目录下新建merge_av.cfg文本文件,第一行保存视频编码,第二行保存音频编码;linux 下,merge_av.cfg保存在HOME目录。
如果要修改,windows下在merge_av.exe同目录下新建merge_av.cfg文本文件,第一行保存视频编码,第二行保存音频编码;linux 下,merge_av.cfg保存在HOME目录下的merge_av目录。
对于录像文件只有音频的情况,merge_av把音频与blank.jpg合并成视频文件。blank.jpg 在windows系统里,与merge_av.exe放在同一目录。在linux下,放在HOME目录下的merge_av目录。
例如:
uid_61712260_20171122130331754.txt内容如下:
0.000 61712260_20171122130331753.aac create
1366.801 61712260_20171122130331753.aac close
运行mergeav uid_61712260_20171122130331754.txt执行的命令是,linux下,HOME目录为/home/hubin
windows(假定merge_av.exe在d:\Merge_av目录):
ffmpeg -y -loop 1 -i D:\Merge_av\blank.jpg -i 61712260_20171122130331753.aac -loop 0 -shortest -acodec copy -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 dest.ts
linux(linux下,假定HOME目录为/home/hubin):
ffmpeg -y -loop 1 -i /home/hubin/merge_av/blank.jpg -i 61712260_20171122130331753.aac -loop 0 -shortest -acodec copy -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 dest.ts
... ...
... ... @@ -36,14 +36,48 @@ void run_shell_cmd(const char * cmd)
}
}
#ifdef WIN32
#include <Windows.h>
char exe_path[MAX_PATH] = { 0 };
int GetExePath()
{
char path_buffer[MAX_PATH] = "";
char drive[32] = "";
char dir[256] = "";
char fname[64] = "";
char ext[32] = "";
GetModuleFileNameA(NULL, path_buffer, 256);
_splitpath(path_buffer, drive, dir, fname, ext);
strcpy(exe_path, drive);
strcat(exe_path, dir);
return 0;
}
#endif
char cfg_path[1024];
void get_config_path(){
#ifdef WIN32
GetExePath();
strcpy(cfg_path, exe_path);
#else
strcpy(cfg_path, getenv("HOME"));
strcat(cfg_path, "/merge_av/");
#endif
}
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];
void addinfo( string t, string name, bool bstart ){
media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? type_audio : type_video;
void addinfo(string t, string name, bool bstart){
media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? type_audio : type_video;
if (bstart) {
fileinfo f;
f.start_time = atof(t.c_str());
... ... @@ -105,7 +139,14 @@ void get_video_first_frame_jpeg(fileinfo video, char * destfile)
void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -loop 1 -i %s.jpg -i %d_%s -loop 0 -shortest %s %s %s", video.name.c_str(), nf, audio.name.c_str(),acodec_param, vcodec_param, destfile);
sprintf(buf, "ffmpeg -y -loop 1 -i %s.jpg -i %d_%s -loop 0 -shortest %s %s %s", video.name.c_str(), 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];
sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio.name.c_str(), acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
... ... @@ -140,7 +181,7 @@ void concate_files(vector<string > merged_files, const char * destfile)
run_shell_cmd(buf);
}
void adjust_dest_timecode(const char * src , const char * dest)
void adjust_dest_timecode(const char * src, const char * dest)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -acodec copy -vcodec copy %s", src, dest);
... ... @@ -172,61 +213,86 @@ int process_files(const char * output_dest_file)
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) {
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;
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++;
get_config_path();
if (filesvideo.size()) {//has video files
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) {
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;
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
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);
megre_audio_video(audio, nf, video, destfile);
merged_files.push_back(destfile);
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 (nv != filesvideo.size() - 1) {// not the last one
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);
megre_audio_video(audio, nf, video, destfile);
merged_files.push_back(destfile);
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);
}
}
else {//only audio
char blank_pic_file[1024];
audio_start = video.end_time;
strcpy(blank_pic_file, cfg_path);
strcat(blank_pic_file, "blank.jpg");
sprintf(destfile, "%d.ts", nf);
megre_audio_video(audio, nf, video, destfile);
merged_files.push_back(destfile);
nf++;
}
if (filesaudio.size() == 1){
fileinfo audio = filesaudio[0];
merge_audio_pic(audio, blank_pic_file, "dest.ts");
return 0;
}
for (int i = 0; i < filesaudio.size(); i++){
fileinfo audio = filesaudio[i];
sprintf(destfile, "%d.ts", nf);
merge_audio_pic(audio, blank_pic_file, destfile);
merged_files.push_back(destfile);
nf++;
}
}
concate_files(merged_files , "m.ts");
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);
... ... @@ -260,27 +326,6 @@ int readfile(char * filename)
return 0;
}
#ifdef WIN32
#include <Windows.h>
char exe_path[MAX_PATH] = { 0 };
int GetExePath()
{
char path_buffer[MAX_PATH] = "";
char drive[32] = "";
char dir[256] = "";
char fname[64] = "";
char ext[32] = "";
GetModuleFileNameA(NULL, path_buffer, 256);
_splitpath(path_buffer, drive, dir, fname, ext);
strcpy(exe_path, drive);
strcat(exe_path, dir);
return 0;
}
#endif
void load_codec_param()
{
... ... @@ -288,14 +333,10 @@ void load_codec_param()
strcpy(vcodec_param, default_vcodec_param);
char cfgfile[1024];
#ifdef WIN32
GetExePath();
strcpy(cfgfile, exe_path);
#else
strcpy(cfgfile ,getenv("HOME"));
strcat(cfgfile ,"/");
#endif
strcpy(cfgfile, cfg_path);
strcat(cfgfile, "merge_av.cfg");
ifstream fin(cfgfile);
if (!fin) {
return;
... ...
0.000 61712260_20171122130331753.aac create
1366.801 61712260_20171122130331753.aac close
... ...