胡斌

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 @@ -50,7 +50,23 @@ ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts
50 50
51 调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 51 调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20
52 音频编码参数是: -acodec copy 52 音频编码参数是: -acodec copy
53 -如果要修改,windows下在merge_av.exe同目录下新建merge_av.cfg文本文件,第一行保存视频编码,第二行保存音频编码;linux 下,merge_av.cfg保存在HOME目录。 53 +如果要修改,windows下在merge_av.exe同目录下新建merge_av.cfg文本文件,第一行保存视频编码,第二行保存音频编码;linux 下,merge_av.cfg保存在HOME目录下的merge_av目录。
  54 +
  55 +对于录像文件只有音频的情况,merge_av把音频与blank.jpg合并成视频文件。blank.jpg 在windows系统里,与merge_av.exe放在同一目录。在linux下,放在HOME目录下的merge_av目录。
  56 +例如:
  57 +uid_61712260_20171122130331754.txt内容如下:
  58 +
  59 +0.000 61712260_20171122130331753.aac create
  60 +1366.801 61712260_20171122130331753.aac close
  61 +
  62 +运行mergeav uid_61712260_20171122130331754.txt执行的命令是,linux下,HOME目录为/home/hubin
  63 +windows(假定merge_av.exe在d:\Merge_av目录):
  64 +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
  65 +linux(linux下,假定HOME目录为/home/hubin):
  66 +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
  67 +
  68 +
  69 +
54 70
55 71
56 72
@@ -36,14 +36,48 @@ void run_shell_cmd(const char * cmd) @@ -36,14 +36,48 @@ void run_shell_cmd(const char * cmd)
36 } 36 }
37 } 37 }
38 38
  39 +#ifdef WIN32
  40 +#include <Windows.h>
  41 +
  42 +char exe_path[MAX_PATH] = { 0 };
  43 +
  44 +int GetExePath()
  45 +{
  46 + char path_buffer[MAX_PATH] = "";
  47 + char drive[32] = "";
  48 + char dir[256] = "";
  49 + char fname[64] = "";
  50 + char ext[32] = "";
  51 +
  52 + GetModuleFileNameA(NULL, path_buffer, 256);
  53 + _splitpath(path_buffer, drive, dir, fname, ext);
  54 +
  55 + strcpy(exe_path, drive);
  56 + strcat(exe_path, dir);
  57 + return 0;
  58 +}
  59 +#endif
  60 +
  61 +char cfg_path[1024];
  62 +
  63 +void get_config_path(){
  64 +#ifdef WIN32
  65 + GetExePath();
  66 + strcpy(cfg_path, exe_path);
  67 +#else
  68 + strcpy(cfg_path, getenv("HOME"));
  69 + strcat(cfg_path, "/merge_av/");
  70 +#endif
  71 +}
  72 +
39 const char * default_vcodec_param = "-vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20"; 73 const char * default_vcodec_param = "-vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20";
40 const char * default_acodec_param = "-acodec copy"; 74 const char * default_acodec_param = "-acodec copy";
41 75
42 char vcodec_param[1024]; 76 char vcodec_param[1024];
43 char acodec_param[1024]; 77 char acodec_param[1024];
44 78
45 -void addinfo( string t, string name, bool bstart ){  
46 - media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? type_audio : type_video; 79 +void addinfo(string t, string name, bool bstart){
  80 + media_type mtype = name.substr(name.length() - 4, name.length()) == ".aac" ? type_audio : type_video;
47 if (bstart) { 81 if (bstart) {
48 fileinfo f; 82 fileinfo f;
49 f.start_time = atof(t.c_str()); 83 f.start_time = atof(t.c_str());
@@ -105,7 +139,14 @@ void get_video_first_frame_jpeg(fileinfo video, char * destfile) @@ -105,7 +139,14 @@ void get_video_first_frame_jpeg(fileinfo video, char * destfile)
105 void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfile) 139 void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfile)
106 { 140 {
107 char buf[2048]; 141 char buf[2048];
108 - 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); 142 + 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);
  143 + run_shell_cmd(buf);
  144 +}
  145 +
  146 +void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile)
  147 +{
  148 + char buf[2048];
  149 + 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);
109 run_shell_cmd(buf); 150 run_shell_cmd(buf);
110 } 151 }
111 152
@@ -140,7 +181,7 @@ void concate_files(vector<string > merged_files, const char * destfile) @@ -140,7 +181,7 @@ void concate_files(vector<string > merged_files, const char * destfile)
140 run_shell_cmd(buf); 181 run_shell_cmd(buf);
141 } 182 }
142 183
143 -void adjust_dest_timecode(const char * src , const char * dest) 184 +void adjust_dest_timecode(const char * src, const char * dest)
144 { 185 {
145 char buf[2048]; 186 char buf[2048];
146 sprintf(buf, "ffmpeg -y -i %s -acodec copy -vcodec copy %s", src, dest); 187 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) @@ -172,61 +213,86 @@ int process_files(const char * output_dest_file)
172 int nf = 0; 213 int nf = 0;
173 char destfile[1024]; 214 char destfile[1024];
174 215
175 - for (int i = 0; i < filesaudio.size(); i++){ //  
176 - fileinfo audio = filesaudio[i];  
177 - float audio_start = 0;  
178 -  
179 - for (; nv < filesvideo.size(); nv++) {  
180 - fileinfo video = filesvideo[nv];  
181 -  
182 - if (video.start_time - audio_start > 0.100) {  
183 -  
184 - sprintf( destfile, "%d_%s", nf, audio.name.c_str() );  
185 - split_audio(audio, audio_start, video.start_time - audio_start, destfile);  
186 - tmp_files.push_back(destfile);  
187 -  
188 - audio_start = video.start_time;  
189 -  
190 - sprintf(destfile, "%s.jpg", video.name.c_str());  
191 - get_video_first_frame_jpeg(video, destfile);  
192 - tmp_files.push_back(destfile);  
193 -  
194 - sprintf(destfile, "%d.ts", nf);  
195 - merge_audio_pic(audio, nf, video, destfile);  
196 - merged_files.push_back(destfile);  
197 - nf++; 216 + get_config_path();
  217 +
  218 + if (filesvideo.size()) {//has video files
  219 + for (int i = 0; i < filesaudio.size(); i++){ //
  220 + fileinfo audio = filesaudio[i];
  221 + float audio_start = 0;
  222 +
  223 + for (; nv < filesvideo.size(); nv++) {
  224 + fileinfo video = filesvideo[nv];
  225 +
  226 + if (video.start_time - audio_start > 0.100) {
  227 +
  228 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
  229 + split_audio(audio, audio_start, video.start_time - audio_start, destfile);
  230 + tmp_files.push_back(destfile);
  231 +
  232 + audio_start = video.start_time;
  233 +
  234 + sprintf(destfile, "%s.jpg", video.name.c_str());
  235 + get_video_first_frame_jpeg(video, destfile);
  236 + tmp_files.push_back(destfile);
  237 +
  238 + sprintf(destfile, "%d.ts", nf);
  239 + merge_audio_pic(audio, nf, video, destfile);
  240 + merged_files.push_back(destfile);
  241 + nf++;
  242 + }
  243 + if (nv != filesvideo.size() - 1) {// not the last one
  244 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
  245 + split_audio(audio, video.start_time, video.end_time - video.start_time, destfile);
  246 + tmp_files.push_back(destfile);
  247 +
  248 + audio_start = video.end_time;
  249 + sprintf(destfile, "%d.ts", nf);
  250 + megre_audio_video(audio, nf, video, destfile);
  251 + merged_files.push_back(destfile);
  252 + nf++;
  253 + }
  254 + else {
  255 + sprintf(destfile, "%d_%s", nf, audio.name.c_str());
  256 + split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile);
  257 + tmp_files.push_back(destfile);
  258 +
  259 + audio_start = video.end_time;
  260 +
  261 + sprintf(destfile, "%d.ts", nf);
  262 + megre_audio_video(audio, nf, video, destfile);
  263 + merged_files.push_back(destfile);
  264 + nf++;
  265 + }
198 } 266 }
199 - if (nv != filesvideo.size() - 1) {// not the last one  
200 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
201 - split_audio(audio, video.start_time, video.end_time - video.start_time, destfile);  
202 - tmp_files.push_back(destfile);  
203 -  
204 - audio_start = video.end_time;  
205 - sprintf(destfile, "%d.ts", nf);  
206 - megre_audio_video(audio, nf, video, destfile);  
207 - merged_files.push_back(destfile);  
208 - nf++;  
209 - }  
210 - else {  
211 - sprintf(destfile, "%d_%s", nf, audio.name.c_str());  
212 - split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile);  
213 - tmp_files.push_back(destfile); 267 + }
  268 + }
  269 + else {//only audio
  270 + char blank_pic_file[1024];
214 271
215 - audio_start = video.end_time; 272 + strcpy(blank_pic_file, cfg_path);
  273 + strcat(blank_pic_file, "blank.jpg");
216 274
217 - sprintf(destfile, "%d.ts", nf);  
218 - megre_audio_video(audio, nf, video, destfile);  
219 - merged_files.push_back(destfile);  
220 - nf++;  
221 - } 275 + if (filesaudio.size() == 1){
  276 + fileinfo audio = filesaudio[0];
  277 + merge_audio_pic(audio, blank_pic_file, "dest.ts");
  278 + return 0;
  279 + }
  280 +
  281 + for (int i = 0; i < filesaudio.size(); i++){
  282 + fileinfo audio = filesaudio[i];
  283 + sprintf(destfile, "%d.ts", nf);
  284 + merge_audio_pic(audio, blank_pic_file, destfile);
  285 + merged_files.push_back(destfile);
  286 + nf++;
222 } 287 }
223 } 288 }
224 289
225 - concate_files(merged_files , "m.ts"); 290 + concate_files(merged_files, "m.ts");
226 tmp_files.push_back("m.ts"); 291 tmp_files.push_back("m.ts");
227 292
228 adjust_dest_timecode("m.ts", output_dest_file); 293 adjust_dest_timecode("m.ts", output_dest_file);
229 294
  295 +
230 if (!keep_tmp_files) { 296 if (!keep_tmp_files) {
231 removefiles(tmp_files); 297 removefiles(tmp_files);
232 removefiles(merged_files); 298 removefiles(merged_files);
@@ -260,27 +326,6 @@ int readfile(char * filename) @@ -260,27 +326,6 @@ int readfile(char * filename)
260 return 0; 326 return 0;
261 } 327 }
262 328
263 -#ifdef WIN32  
264 -#include <Windows.h>  
265 -  
266 -char exe_path[MAX_PATH] = { 0 };  
267 -  
268 -int GetExePath()  
269 -{  
270 - char path_buffer[MAX_PATH] = "";  
271 - char drive[32] = "";  
272 - char dir[256] = "";  
273 - char fname[64] = "";  
274 - char ext[32] = "";  
275 -  
276 - GetModuleFileNameA(NULL, path_buffer, 256);  
277 - _splitpath(path_buffer, drive, dir, fname, ext);  
278 -  
279 - strcpy(exe_path, drive);  
280 - strcat(exe_path, dir);  
281 - return 0;  
282 -}  
283 -#endif  
284 329
285 void load_codec_param() 330 void load_codec_param()
286 { 331 {
@@ -288,14 +333,10 @@ void load_codec_param() @@ -288,14 +333,10 @@ void load_codec_param()
288 strcpy(vcodec_param, default_vcodec_param); 333 strcpy(vcodec_param, default_vcodec_param);
289 334
290 char cfgfile[1024]; 335 char cfgfile[1024];
291 -#ifdef WIN32  
292 - GetExePath();  
293 - strcpy(cfgfile, exe_path);  
294 -#else  
295 - strcpy(cfgfile ,getenv("HOME"));  
296 - strcat(cfgfile ,"/");  
297 -#endif 336 +
  337 + strcpy(cfgfile, cfg_path);
298 strcat(cfgfile, "merge_av.cfg"); 338 strcat(cfgfile, "merge_av.cfg");
  339 +
299 ifstream fin(cfgfile); 340 ifstream fin(cfgfile);
300 if (!fin) { 341 if (!fin) {
301 return; 342 return;
  1 +0.000 61712260_20171122130331753.aac create
  2 +1366.801 61712260_20171122130331753.aac close