正在显示
1 个修改的文件
包含
65 行增加
和
23 行删除
| @@ -8,6 +8,7 @@ | @@ -8,6 +8,7 @@ | ||
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | bool only_print = false; | 10 | bool only_print = false; |
| 11 | +bool keep_tmp_files = false; | ||
| 11 | using namespace std; | 12 | using namespace std; |
| 12 | 13 | ||
| 13 | class fileinfo { | 14 | class fileinfo { |
| @@ -35,8 +36,8 @@ void run_shell_cmd(const char * cmd) | @@ -35,8 +36,8 @@ void run_shell_cmd(const char * cmd) | ||
| 35 | } | 36 | } |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | -const char * default_vcodec_param = " -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 "; | ||
| 39 | -const char * default_acodec_param = " -acodec copy "; | 39 | +const char * default_vcodec_param = "-vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20"; |
| 40 | +const char * default_acodec_param = "-acodec copy"; | ||
| 40 | 41 | ||
| 41 | char vcodec_param[1024]; | 42 | char vcodec_param[1024]; |
| 42 | char acodec_param[1024]; | 43 | char acodec_param[1024]; |
| @@ -87,17 +88,17 @@ void split(string str, string separator, vector<string> &result, bool includeEmp | @@ -87,17 +88,17 @@ void split(string str, string separator, vector<string> &result, bool includeEmp | ||
| 87 | result.push_back(str.substr(lastPosition, string::npos)); | 88 | result.push_back(str.substr(lastPosition, string::npos)); |
| 88 | } | 89 | } |
| 89 | 90 | ||
| 90 | -void split_audio(fileinfo audio, float audio_start, float duration, int nf) | 91 | +void split_audio(fileinfo audio, float audio_start, float duration, char * destfile) |
| 91 | { | 92 | { |
| 92 | char buf[2048]; | 93 | char buf[2048]; |
| 93 | - 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()); | 94 | + sprintf(buf, "ffmpeg -y -i %s -ss %.3f -t %.3f %s %s", audio.name.c_str(), audio_start, duration, acodec_param, destfile); |
| 94 | run_shell_cmd(buf); | 95 | run_shell_cmd(buf); |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | -void get_video_firstframe(fileinfo video) | 98 | +void get_video_first_frame_jpeg(fileinfo video, char * destfile) |
| 98 | { | 99 | { |
| 99 | char buf[2048]; | 100 | char buf[2048]; |
| 100 | - sprintf(buf, "ffmpeg -y -i %s -vframes 1 -ss 0 -f mjpeg -an %s.jpg", video.name.c_str(), video.name.c_str()); | 101 | + sprintf(buf, "ffmpeg -y -i %s -vframes 1 -ss 0 -f mjpeg -an %s", video.name.c_str(), destfile); |
| 101 | run_shell_cmd(buf); | 102 | run_shell_cmd(buf); |
| 102 | } | 103 | } |
| 103 | 104 | ||
| @@ -115,7 +116,7 @@ void megre_audio_video(fileinfo audio, int nf, fileinfo video, char * destfile) | @@ -115,7 +116,7 @@ void megre_audio_video(fileinfo audio, int nf, fileinfo video, char * destfile) | ||
| 115 | run_shell_cmd(buf); | 116 | run_shell_cmd(buf); |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 118 | -void concate_files(vector<string > merged_files) | 119 | +void concate_files(vector<string > merged_files, char * destfile) |
| 119 | { | 120 | { |
| 120 | char buf[2048]; | 121 | char buf[2048]; |
| 121 | #ifdef WIN32 | 122 | #ifdef WIN32 |
| @@ -125,6 +126,7 @@ void concate_files(vector<string > merged_files) | @@ -125,6 +126,7 @@ void concate_files(vector<string > merged_files) | ||
| 125 | strcat(buf, " + /B "); | 126 | strcat(buf, " + /B "); |
| 126 | strcat(buf, merged_files[i].c_str()); | 127 | strcat(buf, merged_files[i].c_str()); |
| 127 | } | 128 | } |
| 129 | + strcat(buf, " "); | ||
| 128 | #else | 130 | #else |
| 129 | strcpy(buf, "cat "); | 131 | strcpy(buf, "cat "); |
| 130 | 132 | ||
| @@ -132,41 +134,64 @@ void concate_files(vector<string > merged_files) | @@ -132,41 +134,64 @@ void concate_files(vector<string > merged_files) | ||
| 132 | strcat(buf, " "); | 134 | strcat(buf, " "); |
| 133 | strcat(buf, merged_files[i].c_str()); | 135 | strcat(buf, merged_files[i].c_str()); |
| 134 | } | 136 | } |
| 135 | - strcat(buf, ">"); | 137 | + strcat(buf, "> "); |
| 136 | #endif // WIN32 | 138 | #endif // WIN32 |
| 137 | - strcat(buf, " m.ts"); | 139 | + strcat(buf, destfile); |
| 138 | run_shell_cmd(buf); | 140 | run_shell_cmd(buf); |
| 139 | } | 141 | } |
| 140 | 142 | ||
| 141 | -void adjust_dest_timecode() | 143 | +void adjust_dest_timecode(char * src , char * dest) |
| 142 | { | 144 | { |
| 143 | - run_shell_cmd("ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts"); | 145 | + char buf[2048]; |
| 146 | + sprintf(buf, "ffmpeg -y -i %s -acodec copy -vcodec copy %s", src, dest); | ||
| 147 | + run_shell_cmd(buf); | ||
| 148 | +} | ||
| 149 | + | ||
| 150 | +void removefiles(vector<string> files) | ||
| 151 | +{ | ||
| 152 | + for (int i = 0; i < files.size(); i++) { | ||
| 153 | + _unlink(files[i].c_str()); | ||
| 154 | + } | ||
| 144 | } | 155 | } |
| 145 | 156 | ||
| 146 | -int process_files() | 157 | +int process_files(char * output_dest_file) |
| 147 | { | 158 | { |
| 148 | vector<fileinfo> & filesaudio = media_files[type_audio]; | 159 | vector<fileinfo> & filesaudio = media_files[type_audio]; |
| 149 | vector<fileinfo> & filesvideo = media_files[type_video]; | 160 | vector<fileinfo> & filesvideo = media_files[type_video]; |
| 150 | vector<string > merged_files; | 161 | vector<string > merged_files; |
| 162 | + vector<string> tmp_files; | ||
| 151 | int nv = 0; | 163 | int nv = 0; |
| 152 | int nf = 0; | 164 | int nf = 0; |
| 153 | char destfile[1024]; | 165 | char destfile[1024]; |
| 166 | + | ||
| 154 | for (int i = 0; i < filesaudio.size(); i++){ // | 167 | for (int i = 0; i < filesaudio.size(); i++){ // |
| 155 | fileinfo audio = filesaudio[i]; | 168 | fileinfo audio = filesaudio[i]; |
| 156 | float audio_start = 0; | 169 | float audio_start = 0; |
| 170 | + | ||
| 157 | for (; nv < filesvideo.size(); nv++) { | 171 | for (; nv < filesvideo.size(); nv++) { |
| 158 | fileinfo video = filesvideo[nv]; | 172 | fileinfo video = filesvideo[nv]; |
| 173 | + | ||
| 159 | if (video.start_time - audio_start > 0.100) { | 174 | if (video.start_time - audio_start > 0.100) { |
| 160 | - split_audio(audio, audio_start, video.start_time - audio_start, nf); | 175 | + |
| 176 | + sprintf( destfile, "%d_%s", nf, audio.name.c_str() ); | ||
| 177 | + split_audio(audio, audio_start, video.start_time - audio_start, destfile); | ||
| 178 | + tmp_files.push_back(destfile); | ||
| 179 | + | ||
| 161 | audio_start = video.start_time; | 180 | audio_start = video.start_time; |
| 162 | - get_video_firstframe(video); | 181 | + |
| 182 | + sprintf(destfile, "%s.jpg", video.name.c_str()); | ||
| 183 | + get_video_first_frame_jpeg(video, destfile); | ||
| 184 | + tmp_files.push_back(destfile); | ||
| 185 | + | ||
| 163 | sprintf(destfile, "%d.ts", nf); | 186 | sprintf(destfile, "%d.ts", nf); |
| 164 | merge_audio_pic(audio, nf, video, destfile); | 187 | merge_audio_pic(audio, nf, video, destfile); |
| 165 | merged_files.push_back(destfile); | 188 | merged_files.push_back(destfile); |
| 166 | nf++; | 189 | nf++; |
| 167 | } | 190 | } |
| 168 | if (nv != filesvideo.size() - 1) {// not the last one | 191 | if (nv != filesvideo.size() - 1) {// not the last one |
| 169 | - split_audio(audio, video.start_time, video.end_time - video.start_time, nf); | 192 | + sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 193 | + split_audio(audio, video.start_time, video.end_time - video.start_time, destfile); | ||
| 194 | + tmp_files.push_back(destfile); | ||
| 170 | 195 | ||
| 171 | audio_start = video.end_time; | 196 | audio_start = video.end_time; |
| 172 | sprintf(destfile, "%d.ts", nf); | 197 | sprintf(destfile, "%d.ts", nf); |
| @@ -175,8 +200,12 @@ int process_files() | @@ -175,8 +200,12 @@ int process_files() | ||
| 175 | nf++; | 200 | nf++; |
| 176 | } | 201 | } |
| 177 | else { | 202 | else { |
| 178 | - split_audio(audio, video.start_time, audio.end_time - video.start_time, nf); | 203 | + sprintf(destfile, "%d_%s", nf, audio.name.c_str()); |
| 204 | + split_audio(audio, video.start_time, audio.end_time - video.start_time, destfile); | ||
| 205 | + tmp_files.push_back(destfile); | ||
| 206 | + | ||
| 179 | audio_start = video.end_time; | 207 | audio_start = video.end_time; |
| 208 | + | ||
| 180 | sprintf(destfile, "%d.ts", nf); | 209 | sprintf(destfile, "%d.ts", nf); |
| 181 | megre_audio_video(audio, nf, video, destfile); | 210 | megre_audio_video(audio, nf, video, destfile); |
| 182 | merged_files.push_back(destfile); | 211 | merged_files.push_back(destfile); |
| @@ -185,8 +214,16 @@ int process_files() | @@ -185,8 +214,16 @@ int process_files() | ||
| 185 | } | 214 | } |
| 186 | } | 215 | } |
| 187 | 216 | ||
| 188 | - concate_files(merged_files); | ||
| 189 | - adjust_dest_timecode(); | 217 | + concate_files(merged_files , "m.ts"); |
| 218 | + tmp_files.push_back("m.ts"); | ||
| 219 | + | ||
| 220 | + adjust_dest_timecode("m.ts", output_dest_file); | ||
| 221 | + | ||
| 222 | + if (!keep_tmp_files) { | ||
| 223 | + removefiles(tmp_files); | ||
| 224 | + removefiles(merged_files); | ||
| 225 | + } | ||
| 226 | + | ||
| 190 | return 0; | 227 | return 0; |
| 191 | } | 228 | } |
| 192 | 229 | ||
| @@ -276,8 +313,9 @@ int main(int argc, char * argv[]) | @@ -276,8 +313,9 @@ int main(int argc, char * argv[]) | ||
| 276 | if (argc < 2) { | 313 | if (argc < 2) { |
| 277 | printf(" merge_av 1.0.0\n"); | 314 | printf(" merge_av 1.0.0\n"); |
| 278 | printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:"); | 315 | printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:"); |
| 279 | - printf("\n %s record_info_filename [-p]", argv[0]); | 316 | + printf("\n %s record_info_filename [-p] [-k]", argv[0]); |
| 280 | printf("\n -p :only print the command,don't run ffmpeg\n"); | 317 | printf("\n -p :only print the command,don't run ffmpeg\n"); |
| 318 | + printf("\n -k :keep the temp files\n"); | ||
| 281 | return -1; | 319 | return -1; |
| 282 | } | 320 | } |
| 283 | 321 | ||
| @@ -286,14 +324,18 @@ int main(int argc, char * argv[]) | @@ -286,14 +324,18 @@ int main(int argc, char * argv[]) | ||
| 286 | return -2; | 324 | return -2; |
| 287 | } | 325 | } |
| 288 | 326 | ||
| 289 | - | ||
| 290 | - if (argc == 3 && !strcmp(argv[2], "-p")) { | ||
| 291 | - only_print = true; | 327 | + for (int i = 2; i < argc; i++){ |
| 328 | + if (!strcmp(argv[i], "-p")){ | ||
| 329 | + only_print = true; | ||
| 330 | + } | ||
| 331 | + else if (!strcmp(argv[i], "-k")){ | ||
| 332 | + keep_tmp_files = true; | ||
| 333 | + } | ||
| 292 | } | 334 | } |
| 293 | 335 | ||
| 294 | load_codec_param(); | 336 | load_codec_param(); |
| 295 | 337 | ||
| 296 | - process_files(); | 338 | + process_files("dest.ts"); |
| 297 | 339 | ||
| 298 | return 0; | 340 | return 0; |
| 299 | } | 341 | } |
-
请 注册 或 登录 后发表评论