Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
胡斌
/
merge_av
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
胡斌
2017-12-01 07:10:57 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
cf45a1b78af3c7fb68ecdd10a37ce9d802703d75
cf45a1b7
1 parent
1f007146
support keep or delete the temp files,using -k option
隐藏空白字符变更
内嵌
并排对比
正在显示
1 个修改的文件
包含
65 行增加
和
23 行删除
merge_av/merge_av.cpp
merge_av/merge_av.cpp
查看文件 @
cf45a1b
...
...
@@ -8,6 +8,7 @@
bool
only_print
=
false
;
bool
keep_tmp_files
=
false
;
using
namespace
std
;
class
fileinfo
{
...
...
@@ -35,8 +36,8 @@ void run_shell_cmd(const char * cmd)
}
}
const
char
*
default_vcodec_param
=
" -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 "
;
const
char
*
default_acodec_param
=
" -acodec copy "
;
const
char
*
default_vcodec_param
=
"-vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20"
;
const
char
*
default_acodec_param
=
"-acodec copy"
;
char
vcodec_param
[
1024
];
char
acodec_param
[
1024
];
...
...
@@ -87,17 +88,17 @@ void split(string str, string separator, vector<string> &result, bool includeEmp
result
.
push_back
(
str
.
substr
(
lastPosition
,
string
::
npos
));
}
void
split_audio
(
fileinfo
audio
,
float
audio_start
,
float
duration
,
int
nf
)
void
split_audio
(
fileinfo
audio
,
float
audio_start
,
float
duration
,
char
*
destfile
)
{
char
buf
[
2048
];
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
()
);
sprintf
(
buf
,
"ffmpeg -y -i %s -ss %.3f -t %.3f %s %
s"
,
audio
.
name
.
c_str
(),
audio_start
,
duration
,
acodec_param
,
destfile
);
run_shell_cmd
(
buf
);
}
void
get_video_first
frame
(
fileinfo
video
)
void
get_video_first
_frame_jpeg
(
fileinfo
video
,
char
*
destfile
)
{
char
buf
[
2048
];
sprintf
(
buf
,
"ffmpeg -y -i %s -vframes 1 -ss 0 -f mjpeg -an %s
.jpg"
,
video
.
name
.
c_str
(),
video
.
name
.
c_str
()
);
sprintf
(
buf
,
"ffmpeg -y -i %s -vframes 1 -ss 0 -f mjpeg -an %s
"
,
video
.
name
.
c_str
(),
destfile
);
run_shell_cmd
(
buf
);
}
...
...
@@ -115,7 +116,7 @@ void megre_audio_video(fileinfo audio, int nf, fileinfo video, char * destfile)
run_shell_cmd
(
buf
);
}
void
concate_files
(
vector
<
string
>
merged_files
)
void
concate_files
(
vector
<
string
>
merged_files
,
char
*
destfile
)
{
char
buf
[
2048
];
#ifdef WIN32
...
...
@@ -125,6 +126,7 @@ void concate_files(vector<string > merged_files)
strcat
(
buf
,
" + /B "
);
strcat
(
buf
,
merged_files
[
i
].
c_str
());
}
strcat
(
buf
,
" "
);
#else
strcpy
(
buf
,
"cat "
);
...
...
@@ -132,41 +134,64 @@ void concate_files(vector<string > merged_files)
strcat
(
buf
,
" "
);
strcat
(
buf
,
merged_files
[
i
].
c_str
());
}
strcat
(
buf
,
">"
);
strcat
(
buf
,
">
"
);
#endif // WIN32
strcat
(
buf
,
" m.ts"
);
strcat
(
buf
,
destfile
);
run_shell_cmd
(
buf
);
}
void
adjust_dest_timecode
()
void
adjust_dest_timecode
(
char
*
src
,
char
*
dest
)
{
run_shell_cmd
(
"ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts"
);
char
buf
[
2048
];
sprintf
(
buf
,
"ffmpeg -y -i %s -acodec copy -vcodec copy %s"
,
src
,
dest
);
run_shell_cmd
(
buf
);
}
void
removefiles
(
vector
<
string
>
files
)
{
for
(
int
i
=
0
;
i
<
files
.
size
();
i
++
)
{
_unlink
(
files
[
i
].
c_str
());
}
}
int
process_files
()
int
process_files
(
char
*
output_dest_file
)
{
vector
<
fileinfo
>
&
filesaudio
=
media_files
[
type_audio
];
vector
<
fileinfo
>
&
filesvideo
=
media_files
[
type_video
];
vector
<
string
>
merged_files
;
vector
<
string
>
tmp_files
;
int
nv
=
0
;
int
nf
=
0
;
char
destfile
[
1024
];
for
(
int
i
=
0
;
i
<
filesaudio
.
size
();
i
++
){
//
fileinfo
audio
=
filesaudio
[
i
];
float
audio_start
=
0
;
for
(;
nv
<
filesvideo
.
size
();
nv
++
)
{
fileinfo
video
=
filesvideo
[
nv
];
if
(
video
.
start_time
-
audio_start
>
0.100
)
{
split_audio
(
audio
,
audio_start
,
video
.
start_time
-
audio_start
,
nf
);
sprintf
(
destfile
,
"%d_%s"
,
nf
,
audio
.
name
.
c_str
()
);
split_audio
(
audio
,
audio_start
,
video
.
start_time
-
audio_start
,
destfile
);
tmp_files
.
push_back
(
destfile
);
audio_start
=
video
.
start_time
;
get_video_firstframe
(
video
);
sprintf
(
destfile
,
"%s.jpg"
,
video
.
name
.
c_str
());
get_video_first_frame_jpeg
(
video
,
destfile
);
tmp_files
.
push_back
(
destfile
);
sprintf
(
destfile
,
"%d.ts"
,
nf
);
merge_audio_pic
(
audio
,
nf
,
video
,
destfile
);
merged_files
.
push_back
(
destfile
);
nf
++
;
}
if
(
nv
!=
filesvideo
.
size
()
-
1
)
{
// not the last one
split_audio
(
audio
,
video
.
start_time
,
video
.
end_time
-
video
.
start_time
,
nf
);
sprintf
(
destfile
,
"%d_%s"
,
nf
,
audio
.
name
.
c_str
());
split_audio
(
audio
,
video
.
start_time
,
video
.
end_time
-
video
.
start_time
,
destfile
);
tmp_files
.
push_back
(
destfile
);
audio_start
=
video
.
end_time
;
sprintf
(
destfile
,
"%d.ts"
,
nf
);
...
...
@@ -175,8 +200,12 @@ int process_files()
nf
++
;
}
else
{
split_audio
(
audio
,
video
.
start_time
,
audio
.
end_time
-
video
.
start_time
,
nf
);
sprintf
(
destfile
,
"%d_%s"
,
nf
,
audio
.
name
.
c_str
());
split_audio
(
audio
,
video
.
start_time
,
audio
.
end_time
-
video
.
start_time
,
destfile
);
tmp_files
.
push_back
(
destfile
);
audio_start
=
video
.
end_time
;
sprintf
(
destfile
,
"%d.ts"
,
nf
);
megre_audio_video
(
audio
,
nf
,
video
,
destfile
);
merged_files
.
push_back
(
destfile
);
...
...
@@ -185,8 +214,16 @@ int process_files()
}
}
concate_files
(
merged_files
);
adjust_dest_timecode
();
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
);
}
return
0
;
}
...
...
@@ -276,8 +313,9 @@ int main(int argc, char * argv[])
if
(
argc
<
2
)
{
printf
(
" merge_av 1.0.0
\n
"
);
printf
(
" run ffmpeg to merge audio and video files according to the record info file,
\n
usage:"
);
printf
(
"
\n
%s record_info_filename [-p]"
,
argv
[
0
]);
printf
(
"
\n
%s record_info_filename [-p]
[-k]
"
,
argv
[
0
]);
printf
(
"
\n
-p :only print the command,don't run ffmpeg
\n
"
);
printf
(
"
\n
-k :keep the temp files
\n
"
);
return
-
1
;
}
...
...
@@ -286,14 +324,18 @@ int main(int argc, char * argv[])
return
-
2
;
}
if
(
argc
==
3
&&
!
strcmp
(
argv
[
2
],
"-p"
))
{
only_print
=
true
;
for
(
int
i
=
2
;
i
<
argc
;
i
++
){
if
(
!
strcmp
(
argv
[
i
],
"-p"
)){
only_print
=
true
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"-k"
)){
keep_tmp_files
=
true
;
}
}
load_codec_param
();
process_files
();
process_files
(
"dest.ts"
);
return
0
;
}
...
...
请
注册
或
登录
后发表评论