胡斌

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
... ... @@ -22,7 +22,7 @@ merge_av uid_284176605_20171127113703805.txt
964.640 284176605_20171127115308444.webm info width=320 height=240 rotation=0
2770.511 284176605_20171127115308444.webm close
2780.447 284176605_20171127113703805.aac close
1.0.6之前版本:
merge_av读入上述内容,解析后依次执行如下命令,最终生成dest.ts文件,是合成后是音视频文件,(各临时文件缺省删除,可以添加-k 参数保留,如merge_av uid_284176605_20171127113703805.txt -k)
这些命令可以通过 "merge_av uid_284176605_20171127113703805.txt -p"输出查看(-p就是只打印命令,不执行) :
;获取视频视频时长
... ... @@ -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
cat 0.ts 1.ts 2.ts 3.ts 4.ts 5.ts 6.ts 7.ts 8.ts> m.ts ;linux
ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts
1.0.7 读入uid_322641689_20171226123032334.txt,把其中
0.000 322641689_20171226123032334.aac create
0.861 322641689_20171226123033194.webm create
0.861 322641689_20171226123033194.webm info width=320 height=240 rotation=0
44.875 322641689_20171226123032334.aac close
45.178 322641689_20171226123117511.aac create
108.525 322641689_20171226123117511.aac close
109.926 322641689_20171226123033194.webm close
115.278 322641689_20171226123227612.aac create
115.675 322641689_20171226123228008.webm create
115.677 322641689_20171226123228008.webm info width=320 height=240 rotation=0
1669.684 322641689_20171226123228008.webm close
1684.435 322641689_20171226123227612.aac close
生成uid_322641689_20171226123032334_out.txt,其中内容如下,每一行依次为开始时间,时长,合成的ts文件名。
0.000 108.610 322641689_20171226_0.ts
115.278 1569.190 322641689_20171226_1.ts
调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 100 -r 20 -bf 0 -vsync cfr
音频编码参数是: -acodec copy
... ...
... ... @@ -404,6 +404,9 @@ char destfile[1024], audio_file[1024], pic_file[1024];
char blank_pic_file[1024];
char silence_aac_file[1024];
char out_info_file[1024];
FILE * fp_out_info = NULL;
void add_media_info(media_info m)
... ... @@ -603,7 +606,7 @@ int split_audio_for_video(int audio_start,vector<media_info> & result)
for (int i = 0; i <= audio_start + 2; i++){ //remove the infos including the audio end
sorted_infos.pop_front();
}
return 0;
return 1;
}
}
... ... @@ -641,7 +644,7 @@ int split_audio_for_pic(vector<media_info> & result)
sorted_infos.pop_front();
}
sorted_infos.push_front(video);
return 0;
return 1;
}
}
... ... @@ -702,7 +705,7 @@ int process_va()
}
int process_files(const char * output_dest_file)
int process_files_to_1file(const char * output_dest_file)
{
//don't split video, for a video, using merged audios to mix with it
//for audio, mix with video or jpg
... ... @@ -771,12 +774,167 @@ int process_files(const char * output_dest_file)
}
int concate_files_and_adjust_timecode(const char * output_dest_file){
if (merged_files.size() == 1){
printf("\n rename %s to %s", merged_files[0].c_str(), output_dest_file);
rename(merged_files[0].c_str(), output_dest_file);
}
else {
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);
}
merged_files.clear();
tmp_files.clear();
return 0;
}
int get_output_file_name(int i, const char * file_prefix, char * outputfile){
char prefix[24];
strncpy(prefix, file_prefix, 18);
prefix[18] = 0;
sprintf(outputfile, "%s_%d.ts", prefix, i);
return 0;
}
bool is_need_output(int nOutPutFile, vector<media_info> & cur_processing, char * outputfile)
{
if (sorted_infos.size()) {
int lastEnd = cur_processing[cur_processing.size() - 1].type_time;
int nextStart = sorted_infos.front().type_time;
if (nextStart - lastEnd < 0.2) {
return false;
}
}
get_output_file_name(nOutPutFile, cur_processing[0].name.c_str(), outputfile);
return true;
}
void save_out_info(float start_time, char * outputfile)
{
if (fp_out_info) {
float duration = get_file_duration(outputfile, false);
fprintf(fp_out_info, "%.3f %.3f %s\n", start_time, duration, outputfile);
}
}
int process_va_files()
{
char outputfile[1024];
vector<media_info> cur_processing;
int nOutPutFile = 0;
float start_time;
bool is_start = true;
while (sorted_infos.size())
{
media_type mt = sorted_infos[0].m_type;
if (mt == mt_audio){
int index = find_video_between_the_audio();
if (index > 0) //have_video
{
split_audio_for_pic(cur_processing);
}
else {
get_front_info(1, cur_processing);
}
merge_audio_pic(cur_processing);
}
else{
int index = find_video_end();
if (is_audio_start(index - 1)) {
split_audio_for_video(index - 1, cur_processing);
}
else {
get_front_info(index, cur_processing);
}
merge_audio_video(cur_processing);
}
//if the duration between the processed end and the start of not processed is large than 200 ms, reopen a new file
if (is_start){
start_time = cur_processing[0].start_time;
is_start = false;
}
if (is_need_output(nOutPutFile, cur_processing, outputfile)){
nOutPutFile++;
concate_files_and_adjust_timecode(outputfile);
save_out_info(start_time, outputfile);
is_start = true;
}
}
return 0;
}
int process_files()
{
//don't split video, for a video, using merged audios to mix with it
//for audio, mix with video or jpg
char outputfile[1024];
init();
if (!media_files.size()){
return 0;
}
fp_out_info = fopen(out_info_file, "wt");
// judge if it is only one type
media_type mt = media_files[0].m_type;
bool only_one_type = true;
for (int i = 1; i < media_files.size(); i++){
if (mt != media_files[i].m_type){
only_one_type = false;
break;
}
}
if (only_one_type){
if (mt == mt_audio) {
for (int i = 0; i < media_files.size(); i++){
fileinfo audio = media_files[i];
get_output_file_name(i, audio.name.c_str(), outputfile);
merge_audio_pic(audio.name.c_str(), blank_pic_file, outputfile);
save_out_info(audio.start_time, outputfile);
}
}
else {
for (int i = 0; i < media_files.size(); i++){
fileinfo video = media_files[i];
get_output_file_name(i, video.name.c_str(), outputfile);
merge_video_silence(video, silence_aac_file, destfile);
save_out_info(video.start_time, outputfile);
}
}
}
else {
process_va_files();
}
if (fp_out_info) {
fclose(fp_out_info);
}
return 0;
}
int readfile(char * filename)
{
ifstream fin(filename);
if (!fin) {
return -1;
}
const int LINE_LENGTH = 1000;
char str[LINE_LENGTH];
while (fin.getline(str, LINE_LENGTH))
... ... @@ -826,10 +984,20 @@ void load_codec_param()
}
}
void get_outinfo_file_name(char * input)
{
strcpy(out_info_file, input);
char * p = strstr(out_info_file, ".");
if (p) {
*p = 0;
}
strcat(out_info_file, "_out.txt");
}
int main(int argc, char * argv[])
{
if (argc < 2) {
printf(" merge_av 1.0.6\n");
printf(" merge_av 1.0.7\n");
printf(" run ffmpeg to merge audio and video files according to the record info file,\nusage:");
printf("\n %s record_info_filename [-p] [-k]", argv[0]);
printf("\n -p :only print the command,don't run ffmpeg");
... ... @@ -851,11 +1019,13 @@ int main(int argc, char * argv[])
}
}
get_outinfo_file_name(argv[1]);
get_config_path();
load_codec_param();
process_files("dest.ts");
process_files();
return 0;
}
... ...