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
正在显示
1 个修改的文件
包含
153 行增加
和
19 行删除
| @@ -28,10 +28,9 @@ vector<fileinfo> media_files[2]; | @@ -28,10 +28,9 @@ vector<fileinfo> media_files[2]; | ||
| 28 | 28 | ||
| 29 | void run_shell_cmd(const char * cmd) | 29 | void run_shell_cmd(const char * cmd) |
| 30 | { | 30 | { |
| 31 | - if (only_print){ | ||
| 32 | - printf("%s\n", cmd); | ||
| 33 | - } | ||
| 34 | - else { | 31 | + printf("run command:%s\n", cmd); |
| 32 | + | ||
| 33 | + if (!only_print){ | ||
| 35 | system(cmd); | 34 | system(cmd); |
| 36 | } | 35 | } |
| 37 | } | 36 | } |
| @@ -154,6 +153,13 @@ void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfi | @@ -154,6 +153,13 @@ void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfi | ||
| 154 | run_shell_cmd(buf); | 153 | run_shell_cmd(buf); |
| 155 | } | 154 | } |
| 156 | 155 | ||
| 156 | +void merge_audio_pic(fileinfo audio, int nf, const char * picfile, const char * destfile) | ||
| 157 | +{ | ||
| 158 | + char buf[2048]; | ||
| 159 | + 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); | ||
| 160 | + run_shell_cmd(buf); | ||
| 161 | +} | ||
| 162 | + | ||
| 157 | void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile) | 163 | void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile) |
| 158 | { | 164 | { |
| 159 | char buf[2048]; | 165 | char buf[2048]; |
| @@ -203,15 +209,117 @@ void adjust_dest_timecode(const char * src, const char * dest) | @@ -203,15 +209,117 @@ void adjust_dest_timecode(const char * src, const char * dest) | ||
| 203 | #include <unistd.h> | 209 | #include <unistd.h> |
| 204 | #endif | 210 | #endif |
| 205 | 211 | ||
| 212 | + | ||
| 213 | +void remove_file(const char * file) | ||
| 214 | +{ | ||
| 215 | +#ifdef WIN32 | ||
| 216 | + _unlink(file); | ||
| 217 | +#else | ||
| 218 | + unlink(file); | ||
| 219 | +#endif | ||
| 220 | +} | ||
| 221 | + | ||
| 206 | void removefiles(vector<string> files) | 222 | void removefiles(vector<string> files) |
| 207 | { | 223 | { |
| 208 | for (int i = 0; i < files.size(); i++) { | 224 | for (int i = 0; i < files.size(); i++) { |
| 225 | + remove_file(files[i].c_str()); | ||
| 226 | + } | ||
| 227 | +} | ||
| 228 | + | ||
| 229 | +float get_video_duration(const char *videofile) | ||
| 230 | +{ | ||
| 231 | + char buf[2048]; | ||
| 232 | + sprintf(buf, "ffmpeg -y -i %s -vcodec copy -an %s.mkv", videofile, videofile); | ||
| 233 | + run_shell_cmd(buf); | ||
| 209 | #ifdef WIN32 | 234 | #ifdef WIN32 |
| 210 | - _unlink(files[i].c_str()); | 235 | + sprintf(buf, "ffmpeg -i %s.mkv > %s.txt 2>&1", videofile, videofile); |
| 211 | #else | 236 | #else |
| 212 | - unlink(files[i].c_str()); | 237 | + sprintf(buf, "ffmpeg -i %s.mkv &> %s.txt", videofile, videofile); |
| 213 | #endif | 238 | #endif |
| 239 | + | ||
| 240 | + run_shell_cmd(buf); | ||
| 241 | + | ||
| 242 | + if (!keep_tmp_files) { | ||
| 243 | + char buf[2048]; | ||
| 244 | + sprintf(buf, "%s.mkv", videofile); | ||
| 245 | + remove_file(buf); | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + sprintf(buf, "%s.txt", videofile); | ||
| 249 | + | ||
| 250 | + FILE * fp = fopen(buf, "rb"); | ||
| 251 | + if (!fp) { | ||
| 252 | + printf("\nOpen %s error\n", buf); | ||
| 253 | + return -1.0; | ||
| 254 | + } | ||
| 255 | + fseek(fp, 0l, SEEK_END); | ||
| 256 | + long file_len = ftell(fp); | ||
| 257 | + fseek(fp, 0l, SEEK_SET); | ||
| 258 | + | ||
| 259 | + char * content = new char[file_len + 1]; | ||
| 260 | + fread(content, 1, file_len, fp); | ||
| 261 | + fclose(fp); | ||
| 262 | + | ||
| 263 | + content[file_len] = 0; | ||
| 264 | + | ||
| 265 | + if (!keep_tmp_files) { | ||
| 266 | + char buf[2048]; | ||
| 267 | + sprintf(buf, "%s.txt", videofile); | ||
| 268 | + remove_file(buf); | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + char * pDuration = strstr(content, "Duration:"); | ||
| 272 | + if (!pDuration){ | ||
| 273 | + printf("\ncan't find 'Duration:' in %s\n", buf); | ||
| 274 | + delete content; | ||
| 275 | + return -1.0; | ||
| 214 | } | 276 | } |
| 277 | + | ||
| 278 | + char * pComma = strstr(pDuration, ","); | ||
| 279 | + if (!pDuration){ | ||
| 280 | + printf("\ncan't find ',' after 'Duration:' in %s\n", buf); | ||
| 281 | + delete content; | ||
| 282 | + return -1.0; | ||
| 283 | + } | ||
| 284 | + int len = pComma - pDuration - 10; | ||
| 285 | + strncpy(content, pDuration + 10, len); | ||
| 286 | + content[len] = 0; | ||
| 287 | + vector <string > hms; | ||
| 288 | + split(content, ":", hms); | ||
| 289 | + | ||
| 290 | + delete content; | ||
| 291 | + | ||
| 292 | + if (hms.size() != 3) { | ||
| 293 | + printf("\nerro parsing duration in %s, the duration string is:%s\n", buf, content); | ||
| 294 | + delete content; | ||
| 295 | + return -1.0; | ||
| 296 | + } | ||
| 297 | + | ||
| 298 | + int hour = atoi(hms[0].c_str()); | ||
| 299 | + int minute = atoi(hms[1].c_str()); | ||
| 300 | + float sec = atof(hms[2].c_str()); | ||
| 301 | + | ||
| 302 | + return hour * 3600 + minute * 60 + sec; | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +void get_duration_from_video_file() | ||
| 306 | +{ | ||
| 307 | + vector<fileinfo> & filesvideo = media_files[type_video]; | ||
| 308 | + vector<string> tmp_files; | ||
| 309 | + | ||
| 310 | + bool tmp = only_print; | ||
| 311 | + only_print = false; | ||
| 312 | + | ||
| 313 | + | ||
| 314 | + for (int i = 0; i < filesvideo.size(); i++){ | ||
| 315 | + float duration = get_video_duration(filesvideo[i].name.c_str()); | ||
| 316 | + if (duration >= 0.10) { | ||
| 317 | + 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); | ||
| 318 | + filesvideo[i].end_time = filesvideo[i].start_time + duration; | ||
| 319 | + } | ||
| 320 | + } | ||
| 321 | + | ||
| 322 | + only_print = tmp; | ||
| 215 | } | 323 | } |
| 216 | 324 | ||
| 217 | int process_files(const char * output_dest_file) | 325 | int process_files(const char * output_dest_file) |
| @@ -223,8 +331,12 @@ int process_files(const char * output_dest_file) | @@ -223,8 +331,12 @@ int process_files(const char * output_dest_file) | ||
| 223 | int nv = 0; | 331 | int nv = 0; |
| 224 | int nf = 0; | 332 | int nf = 0; |
| 225 | char destfile[1024]; | 333 | char destfile[1024]; |
| 334 | + char blank_pic_file[1024]; | ||
| 226 | 335 | ||
| 227 | - get_config_path(); | 336 | + strcpy(blank_pic_file, cfg_path); |
| 337 | + strcat(blank_pic_file, "blank.jpg"); | ||
| 338 | + | ||
| 339 | + get_duration_from_video_file(); | ||
| 228 | 340 | ||
| 229 | if (filesvideo.size()) {//has video files | 341 | if (filesvideo.size()) {//has video files |
| 230 | for (int i = 0; i < filesaudio.size(); i++){ // | 342 | for (int i = 0; i < filesaudio.size(); i++){ // |
| @@ -263,25 +375,45 @@ int process_files(const char * output_dest_file) | @@ -263,25 +375,45 @@ int process_files(const char * output_dest_file) | ||
| 263 | nf++; | 375 | nf++; |
| 264 | } | 376 | } |
| 265 | else { | 377 | else { |
| 378 | + | ||
| 266 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | 379 | sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 267 | - split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile); | ||
| 268 | - tmp_files.push_back(destfile); | ||
| 269 | - | ||
| 270 | - audio_start = video.end_time; | ||
| 271 | 380 | ||
| 272 | - sprintf(destfile, "%d.ts", nf); | ||
| 273 | - megre_audio_video(audio, nf, video, destfile); | ||
| 274 | - merged_files.push_back(destfile); | ||
| 275 | - nf++; | 381 | + if (audio.end_time - video.end_time < 1.0) { |
| 382 | + split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile); | ||
| 383 | + tmp_files.push_back(destfile); | ||
| 384 | + | ||
| 385 | + audio_start = video.end_time; | ||
| 386 | + | ||
| 387 | + sprintf(destfile, "%d.ts", nf); | ||
| 388 | + megre_audio_video(audio, nf, video, destfile); | ||
| 389 | + merged_files.push_back(destfile); | ||
| 390 | + nf++; | ||
| 391 | + } | ||
| 392 | + else{ | ||
| 393 | + split_audio(audio, video.start_time, video.end_time - video.start_time, destfile); | ||
| 394 | + tmp_files.push_back(destfile); | ||
| 395 | + | ||
| 396 | + audio_start = video.end_time; | ||
| 397 | + | ||
| 398 | + sprintf(destfile, "%d.ts", nf); | ||
| 399 | + megre_audio_video(audio, nf, video, destfile); | ||
| 400 | + merged_files.push_back(destfile); | ||
| 401 | + nf++; | ||
| 402 | + | ||
| 403 | + sprintf(destfile, "%d_%s", nf, audio.name.c_str()); | ||
| 404 | + split_audio(audio, video.end_time, audio.end_time - video.end_time, destfile); | ||
| 405 | + tmp_files.push_back(destfile); | ||
| 406 | + | ||
| 407 | + sprintf(destfile, "%d.ts", nf); | ||
| 408 | + merge_audio_pic(audio, nf, blank_pic_file, destfile); | ||
| 409 | + merged_files.push_back(destfile); | ||
| 410 | + nf++; | ||
| 411 | + } | ||
| 276 | } | 412 | } |
| 277 | } | 413 | } |
| 278 | } | 414 | } |
| 279 | } | 415 | } |
| 280 | else {//only audio | 416 | else {//only audio |
| 281 | - char blank_pic_file[1024]; | ||
| 282 | - | ||
| 283 | - strcpy(blank_pic_file, cfg_path); | ||
| 284 | - strcat(blank_pic_file, "blank.jpg"); | ||
| 285 | 417 | ||
| 286 | if (filesaudio.size() == 1){ | 418 | if (filesaudio.size() == 1){ |
| 287 | fileinfo audio = filesaudio[0]; | 419 | fileinfo audio = filesaudio[0]; |
| @@ -393,6 +525,8 @@ int main(int argc, char * argv[]) | @@ -393,6 +525,8 @@ int main(int argc, char * argv[]) | ||
| 393 | } | 525 | } |
| 394 | } | 526 | } |
| 395 | 527 | ||
| 528 | + get_config_path(); | ||
| 529 | + | ||
| 396 | load_codec_param(); | 530 | load_codec_param(); |
| 397 | 531 | ||
| 398 | process_files("dest.ts"); | 532 | process_files("dest.ts"); |
-
请 注册 或 登录 后发表评论