v1.0.7,read recordinfo.txt,transcode ,and save recordinfo_out.txt
in previous version,all the audio an video are merged to one ts, in this version,there are maybe serveral ts,if the video file or audio file is not continuous
正在显示
2 个修改的文件
包含
193 行增加
和
6 行删除
| @@ -22,7 +22,7 @@ merge_av uid_284176605_20171127113703805.txt | @@ -22,7 +22,7 @@ merge_av uid_284176605_20171127113703805.txt | ||
| 22 | 964.640 284176605_20171127115308444.webm info width=320 height=240 rotation=0 | 22 | 964.640 284176605_20171127115308444.webm info width=320 height=240 rotation=0 |
| 23 | 2770.511 284176605_20171127115308444.webm close | 23 | 2770.511 284176605_20171127115308444.webm close |
| 24 | 2780.447 284176605_20171127113703805.aac close | 24 | 2780.447 284176605_20171127113703805.aac close |
| 25 | - | 25 | +1.0.6之前版本: |
| 26 | merge_av读入上述内容,解析后依次执行如下命令,最终生成dest.ts文件,是合成后是音视频文件,(各临时文件缺省删除,可以添加-k 参数保留,如merge_av uid_284176605_20171127113703805.txt -k) | 26 | merge_av读入上述内容,解析后依次执行如下命令,最终生成dest.ts文件,是合成后是音视频文件,(各临时文件缺省删除,可以添加-k 参数保留,如merge_av uid_284176605_20171127113703805.txt -k) |
| 27 | 这些命令可以通过 "merge_av uid_284176605_20171127113703805.txt -p"输出查看(-p就是只打印命令,不执行) : | 27 | 这些命令可以通过 "merge_av uid_284176605_20171127113703805.txt -p"输出查看(-p就是只打印命令,不执行) : |
| 28 | ;获取视频视频时长 | 28 | ;获取视频视频时长 |
| @@ -65,6 +65,23 @@ copy /B 0.ts + /B 1.ts + /B 2.ts + /B 3.ts + /B 4.ts + /B 5.ts + /B 6.ts + /B 7 | @@ -65,6 +65,23 @@ copy /B 0.ts + /B 1.ts + /B 2.ts + /B 3.ts + /B 4.ts + /B 5.ts + /B 6.ts + /B 7 | ||
| 65 | cat 0.ts 1.ts 2.ts 3.ts 4.ts 5.ts 6.ts 7.ts 8.ts> m.ts ;linux | 65 | cat 0.ts 1.ts 2.ts 3.ts 4.ts 5.ts 6.ts 7.ts 8.ts> m.ts ;linux |
| 66 | ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts | 66 | ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts |
| 67 | 67 | ||
| 68 | +1.0.7 读入uid_322641689_20171226123032334.txt,把其中 | ||
| 69 | +0.000 322641689_20171226123032334.aac create | ||
| 70 | +0.861 322641689_20171226123033194.webm create | ||
| 71 | +0.861 322641689_20171226123033194.webm info width=320 height=240 rotation=0 | ||
| 72 | +44.875 322641689_20171226123032334.aac close | ||
| 73 | +45.178 322641689_20171226123117511.aac create | ||
| 74 | +108.525 322641689_20171226123117511.aac close | ||
| 75 | +109.926 322641689_20171226123033194.webm close | ||
| 76 | +115.278 322641689_20171226123227612.aac create | ||
| 77 | +115.675 322641689_20171226123228008.webm create | ||
| 78 | +115.677 322641689_20171226123228008.webm info width=320 height=240 rotation=0 | ||
| 79 | +1669.684 322641689_20171226123228008.webm close | ||
| 80 | +1684.435 322641689_20171226123227612.aac close | ||
| 81 | +生成uid_322641689_20171226123032334_out.txt,其中内容如下,每一行依次为开始时间,时长,合成的ts文件名。 | ||
| 82 | +0.000 108.610 322641689_20171226_0.ts | ||
| 83 | +115.278 1569.190 322641689_20171226_1.ts | ||
| 84 | + | ||
| 68 | 85 | ||
| 69 | 调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 100 -r 20 -bf 0 -vsync cfr | 86 | 调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 100 -r 20 -bf 0 -vsync cfr |
| 70 | 音频编码参数是: -acodec copy | 87 | 音频编码参数是: -acodec copy |
| @@ -404,6 +404,9 @@ char destfile[1024], audio_file[1024], pic_file[1024]; | @@ -404,6 +404,9 @@ char destfile[1024], audio_file[1024], pic_file[1024]; | ||
| 404 | char blank_pic_file[1024]; | 404 | char blank_pic_file[1024]; |
| 405 | char silence_aac_file[1024]; | 405 | char silence_aac_file[1024]; |
| 406 | 406 | ||
| 407 | +char out_info_file[1024]; | ||
| 408 | +FILE * fp_out_info = NULL; | ||
| 409 | + | ||
| 407 | 410 | ||
| 408 | 411 | ||
| 409 | void add_media_info(media_info m) | 412 | void add_media_info(media_info m) |
| @@ -603,7 +606,7 @@ int split_audio_for_video(int audio_start,vector<media_info> & result) | @@ -603,7 +606,7 @@ int split_audio_for_video(int audio_start,vector<media_info> & result) | ||
| 603 | for (int i = 0; i <= audio_start + 2; i++){ //remove the infos including the audio end | 606 | for (int i = 0; i <= audio_start + 2; i++){ //remove the infos including the audio end |
| 604 | sorted_infos.pop_front(); | 607 | sorted_infos.pop_front(); |
| 605 | } | 608 | } |
| 606 | - return 0; | 609 | + return 1; |
| 607 | } | 610 | } |
| 608 | } | 611 | } |
| 609 | 612 | ||
| @@ -641,7 +644,7 @@ int split_audio_for_pic(vector<media_info> & result) | @@ -641,7 +644,7 @@ int split_audio_for_pic(vector<media_info> & result) | ||
| 641 | sorted_infos.pop_front(); | 644 | sorted_infos.pop_front(); |
| 642 | } | 645 | } |
| 643 | sorted_infos.push_front(video); | 646 | sorted_infos.push_front(video); |
| 644 | - return 0; | 647 | + return 1; |
| 645 | } | 648 | } |
| 646 | } | 649 | } |
| 647 | 650 | ||
| @@ -702,7 +705,7 @@ int process_va() | @@ -702,7 +705,7 @@ int process_va() | ||
| 702 | } | 705 | } |
| 703 | 706 | ||
| 704 | 707 | ||
| 705 | -int process_files(const char * output_dest_file) | 708 | +int process_files_to_1file(const char * output_dest_file) |
| 706 | { | 709 | { |
| 707 | //don't split video, for a video, using merged audios to mix with it | 710 | //don't split video, for a video, using merged audios to mix with it |
| 708 | //for audio, mix with video or jpg | 711 | //for audio, mix with video or jpg |
| @@ -771,12 +774,167 @@ int process_files(const char * output_dest_file) | @@ -771,12 +774,167 @@ int process_files(const char * output_dest_file) | ||
| 771 | } | 774 | } |
| 772 | 775 | ||
| 773 | 776 | ||
| 777 | +int concate_files_and_adjust_timecode(const char * output_dest_file){ | ||
| 778 | + if (merged_files.size() == 1){ | ||
| 779 | + printf("\n rename %s to %s", merged_files[0].c_str(), output_dest_file); | ||
| 780 | + rename(merged_files[0].c_str(), output_dest_file); | ||
| 781 | + } | ||
| 782 | + else { | ||
| 783 | + concate_files(merged_files, "m.ts"); | ||
| 784 | + tmp_files.push_back("m.ts"); | ||
| 785 | + | ||
| 786 | + adjust_dest_timecode("m.ts", output_dest_file); | ||
| 787 | + } | ||
| 788 | + | ||
| 789 | + if (!keep_tmp_files) { | ||
| 790 | + removefiles(tmp_files); | ||
| 791 | + removefiles(merged_files); | ||
| 792 | + } | ||
| 793 | + | ||
| 794 | + merged_files.clear(); | ||
| 795 | + tmp_files.clear(); | ||
| 796 | + | ||
| 797 | + return 0; | ||
| 798 | +} | ||
| 799 | + | ||
| 800 | + | ||
| 801 | +int get_output_file_name(int i, const char * file_prefix, char * outputfile){ | ||
| 802 | + char prefix[24]; | ||
| 803 | + strncpy(prefix, file_prefix, 18); | ||
| 804 | + prefix[18] = 0; | ||
| 805 | + sprintf(outputfile, "%s_%d.ts", prefix, i); | ||
| 806 | + return 0; | ||
| 807 | +} | ||
| 808 | + | ||
| 809 | +bool is_need_output(int nOutPutFile, vector<media_info> & cur_processing, char * outputfile) | ||
| 810 | +{ | ||
| 811 | + if (sorted_infos.size()) { | ||
| 812 | + int lastEnd = cur_processing[cur_processing.size() - 1].type_time; | ||
| 813 | + int nextStart = sorted_infos.front().type_time; | ||
| 814 | + if (nextStart - lastEnd < 0.2) { | ||
| 815 | + return false; | ||
| 816 | + } | ||
| 817 | + } | ||
| 818 | + | ||
| 819 | + get_output_file_name(nOutPutFile, cur_processing[0].name.c_str(), outputfile); | ||
| 820 | + return true; | ||
| 821 | +} | ||
| 822 | + | ||
| 823 | +void save_out_info(float start_time, char * outputfile) | ||
| 824 | +{ | ||
| 825 | + if (fp_out_info) { | ||
| 826 | + float duration = get_file_duration(outputfile, false); | ||
| 827 | + fprintf(fp_out_info, "%.3f %.3f %s\n", start_time, duration, outputfile); | ||
| 828 | + } | ||
| 829 | +} | ||
| 830 | + | ||
| 831 | +int process_va_files() | ||
| 832 | +{ | ||
| 833 | + char outputfile[1024]; | ||
| 834 | + vector<media_info> cur_processing; | ||
| 835 | + int nOutPutFile = 0; | ||
| 836 | + float start_time; | ||
| 837 | + bool is_start = true; | ||
| 838 | + | ||
| 839 | + while (sorted_infos.size()) | ||
| 840 | + { | ||
| 841 | + media_type mt = sorted_infos[0].m_type; | ||
| 842 | + if (mt == mt_audio){ | ||
| 843 | + int index = find_video_between_the_audio(); | ||
| 844 | + if (index > 0) //have_video | ||
| 845 | + { | ||
| 846 | + split_audio_for_pic(cur_processing); | ||
| 847 | + } | ||
| 848 | + else { | ||
| 849 | + get_front_info(1, cur_processing); | ||
| 850 | + } | ||
| 851 | + merge_audio_pic(cur_processing); | ||
| 852 | + } | ||
| 853 | + else{ | ||
| 854 | + int index = find_video_end(); | ||
| 855 | + if (is_audio_start(index - 1)) { | ||
| 856 | + split_audio_for_video(index - 1, cur_processing); | ||
| 857 | + } | ||
| 858 | + else { | ||
| 859 | + get_front_info(index, cur_processing); | ||
| 860 | + } | ||
| 861 | + merge_audio_video(cur_processing); | ||
| 862 | + } | ||
| 863 | + //if the duration between the processed end and the start of not processed is large than 200 ms, reopen a new file | ||
| 864 | + if (is_start){ | ||
| 865 | + start_time = cur_processing[0].start_time; | ||
| 866 | + is_start = false; | ||
| 867 | + } | ||
| 868 | + if (is_need_output(nOutPutFile, cur_processing, outputfile)){ | ||
| 869 | + nOutPutFile++; | ||
| 870 | + concate_files_and_adjust_timecode(outputfile); | ||
| 871 | + save_out_info(start_time, outputfile); | ||
| 872 | + is_start = true; | ||
| 873 | + } | ||
| 874 | + } | ||
| 875 | + return 0; | ||
| 876 | +} | ||
| 877 | + | ||
| 878 | + | ||
| 879 | +int process_files() | ||
| 880 | +{ | ||
| 881 | + //don't split video, for a video, using merged audios to mix with it | ||
| 882 | + //for audio, mix with video or jpg | ||
| 883 | + char outputfile[1024]; | ||
| 884 | + init(); | ||
| 885 | + | ||
| 886 | + if (!media_files.size()){ | ||
| 887 | + return 0; | ||
| 888 | + } | ||
| 889 | + | ||
| 890 | + fp_out_info = fopen(out_info_file, "wt"); | ||
| 891 | + | ||
| 892 | + // judge if it is only one type | ||
| 893 | + media_type mt = media_files[0].m_type; | ||
| 894 | + bool only_one_type = true; | ||
| 895 | + for (int i = 1; i < media_files.size(); i++){ | ||
| 896 | + if (mt != media_files[i].m_type){ | ||
| 897 | + only_one_type = false; | ||
| 898 | + break; | ||
| 899 | + } | ||
| 900 | + } | ||
| 901 | + | ||
| 902 | + if (only_one_type){ | ||
| 903 | + if (mt == mt_audio) { | ||
| 904 | + for (int i = 0; i < media_files.size(); i++){ | ||
| 905 | + fileinfo audio = media_files[i]; | ||
| 906 | + get_output_file_name(i, audio.name.c_str(), outputfile); | ||
| 907 | + merge_audio_pic(audio.name.c_str(), blank_pic_file, outputfile); | ||
| 908 | + save_out_info(audio.start_time, outputfile); | ||
| 909 | + } | ||
| 910 | + } | ||
| 911 | + else { | ||
| 912 | + for (int i = 0; i < media_files.size(); i++){ | ||
| 913 | + fileinfo video = media_files[i]; | ||
| 914 | + get_output_file_name(i, video.name.c_str(), outputfile); | ||
| 915 | + merge_video_silence(video, silence_aac_file, destfile); | ||
| 916 | + save_out_info(video.start_time, outputfile); | ||
| 917 | + } | ||
| 918 | + } | ||
| 919 | + } | ||
| 920 | + else { | ||
| 921 | + process_va_files(); | ||
| 922 | + } | ||
| 923 | + | ||
| 924 | + if (fp_out_info) { | ||
| 925 | + fclose(fp_out_info); | ||
| 926 | + } | ||
| 927 | + | ||
| 928 | + return 0; | ||
| 929 | +} | ||
| 930 | + | ||
| 774 | int readfile(char * filename) | 931 | int readfile(char * filename) |
| 775 | { | 932 | { |
| 776 | ifstream fin(filename); | 933 | ifstream fin(filename); |
| 777 | if (!fin) { | 934 | if (!fin) { |
| 778 | return -1; | 935 | return -1; |
| 779 | } | 936 | } |
| 937 | + | ||
| 780 | const int LINE_LENGTH = 1000; | 938 | const int LINE_LENGTH = 1000; |
| 781 | char str[LINE_LENGTH]; | 939 | char str[LINE_LENGTH]; |
| 782 | while (fin.getline(str, LINE_LENGTH)) | 940 | while (fin.getline(str, LINE_LENGTH)) |
| @@ -826,10 +984,20 @@ void load_codec_param() | @@ -826,10 +984,20 @@ void load_codec_param() | ||
| 826 | } | 984 | } |
| 827 | } | 985 | } |
| 828 | 986 | ||
| 987 | +void get_outinfo_file_name(char * input) | ||
| 988 | +{ | ||
| 989 | + strcpy(out_info_file, input); | ||
| 990 | + char * p = strstr(out_info_file, "."); | ||
| 991 | + if (p) { | ||
| 992 | + *p = 0; | ||
| 993 | + } | ||
| 994 | + strcat(out_info_file, "_out.txt"); | ||
| 995 | +} | ||
| 996 | + | ||
| 829 | int main(int argc, char * argv[]) | 997 | int main(int argc, char * argv[]) |
| 830 | { | 998 | { |
| 831 | if (argc < 2) { | 999 | if (argc < 2) { |
| 832 | - printf(" merge_av 1.0.6\n"); | 1000 | + printf(" merge_av 1.0.7\n"); |
| 833 | printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:"); | 1001 | printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:"); |
| 834 | printf("\n %s record_info_filename [-p] [-k]", argv[0]); | 1002 | printf("\n %s record_info_filename [-p] [-k]", argv[0]); |
| 835 | printf("\n -p :only print the command,don't run ffmpeg"); | 1003 | printf("\n -p :only print the command,don't run ffmpeg"); |
| @@ -851,11 +1019,13 @@ int main(int argc, char * argv[]) | @@ -851,11 +1019,13 @@ int main(int argc, char * argv[]) | ||
| 851 | } | 1019 | } |
| 852 | } | 1020 | } |
| 853 | 1021 | ||
| 1022 | + get_outinfo_file_name(argv[1]); | ||
| 1023 | + | ||
| 854 | get_config_path(); | 1024 | get_config_path(); |
| 855 | 1025 | ||
| 856 | load_codec_param(); | 1026 | load_codec_param(); |
| 857 | 1027 | ||
| 858 | - process_files("dest.ts"); | 1028 | + process_files(); |
| 859 | 1029 | ||
| 860 | return 0; | 1030 | return 0; |
| 861 | } | 1031 | } |
-
请 注册 或 登录 后发表评论