胡斌

if -t is not passed in command line, if source file codec is h264,use -vcodec copy when transcode

... ... @@ -84,6 +84,7 @@ ffmpeg -y -i m.ts -acodec copy -vcodec copy dest.ts
调用ffmpeg时,视频编码参数是: -vcodec libx264 -level 3.1 -preset veryfast -g 100 -r 20 -bf 0 -vsync cfr
如果源视频编码是h264,则缺省不做转码,-vcodec copy
音频编码参数是: -acodec copy
如果要修改,windows下在merge_av.exe同目录下新建merge_av.cfg文本文件,第一行保存视频编码,第二行保存音频编码;linux 下,merge_av.cfg保存在HOME目录下的merge_av目录。
... ... @@ -100,6 +101,9 @@ ffmpeg -y -loop 1 -i D:\Merge_av\blank.jpg -i 61712260_20171122130331753.aac -lo
linux(linux下,假定HOME目录为/home/hubin):
ffmpeg -y -loop 1 -i /home/hubin/merge_av/blank.jpg -i 61712260_20171122130331753.aac -loop 0 -shortest -acodec copy -vcodec libx264 -level 3.1 -preset veryfast -g 40 -r 20 dest.ts
-c 把输入文件转换为ts,不做合并处理(输入的是音视频都存在的mp4文件)
-t 如果视频是h264,也做转码。
... ...
... ... @@ -12,6 +12,7 @@
bool only_print = false;
bool keep_tmp_files = false;
bool only_convert_mp4 = false;
bool recodec_h264 = false;
using namespace std;
enum media_type{
... ... @@ -31,6 +32,7 @@ public:
string name;
int index;
media_type m_type;
bool is_h264;
};
class media_info {
... ... @@ -44,6 +46,7 @@ public:
int index;
media_type m_type;
timestamp_type t_type;
bool is_h264;
};
... ... @@ -97,6 +100,7 @@ void get_config_path(){
}
const char * default_vcodec_param = "-vcodec libx264 -level 3.1 -preset veryfast -g 100 -r 20 -bf 0 -vsync cfr";
const char * copy_h264_codec_param = "-vcodec copy";
const char * default_acodec_param = "-acodec copy";
char vcodec_param[1024];
... ... @@ -113,6 +117,7 @@ void addinfo(string t, string name, bool bstart){
f.end_time = f.start_time;
f.name = name;
f.m_type = mtype;
f.is_h264 = false; //default to false when init
if (!first_time_set) {
first_time_set = true;
... ... @@ -178,24 +183,39 @@ void merge_audio_pic(const char * audio, const char * picfile, const char * dest
}
void merge_audio_video(const char * audio, const char * video, const char * destfile)
void merge_audio_video(const char * audio, const char * video, const char * destfile, bool bH264)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile);
if (false == recodec_h264 && true == bH264) {
sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, copy_h264_codec_param, destfile);
}
else {
sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile);
}
run_shell_cmd(buf);
}
void convert_video_to_ts(const char * video, const char * destfile)
void convert_video_to_ts(const char * video, const char * destfile, bool bH264)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s %s %s %s",video, acodec_param, vcodec_param, destfile);
if (false == recodec_h264 && true == bH264) {
sprintf(buf, "ffmpeg -y -i %s %s %s %s", video, acodec_param, copy_h264_codec_param, destfile);
}
else {
sprintf(buf, "ffmpeg -y -i %s %s %s %s", video, acodec_param, vcodec_param, destfile);
}
run_shell_cmd(buf);
}
void merge_video_silence(fileinfo video, const char * aacfile, const char * destfile)
void merge_video_silence(fileinfo video, const char * aacfile, const char * destfile, bool bH264)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -i %s -shortest %s %s %s", aacfile, video.name.c_str(), acodec_param, vcodec_param, destfile);
if (false == recodec_h264 && true == bH264) {
sprintf(buf, "ffmpeg -y -i %s -i %s -shortest %s %s %s", aacfile, video.name.c_str(), acodec_param, copy_h264_codec_param, destfile);
}
else {
sprintf(buf, "ffmpeg -y -i %s -i %s -shortest %s %s %s", aacfile, video.name.c_str(), acodec_param, vcodec_param, destfile);
}
run_shell_cmd(buf);
}
... ... @@ -252,7 +272,7 @@ void removefiles(vector<string> files)
}
}
float parse_ffmpeg_duration(const char * file)
float parse_ffmpeg_duration(const char * file, bool * beH264 = NULL)
{
FILE * fp = fopen(file, "rb");
if (!fp) {
... ... @@ -273,6 +293,16 @@ float parse_ffmpeg_duration(const char * file)
remove_file(file);
}
if (beH264){
*beH264 = false;
char * p = strstr(content, "Video:");
if (p){
if (strstr(p, "h264")){
*beH264 = true;
}
}
}
char * pDuration = strstr(content, "Duration:");
if (!pDuration){
printf("\ncan't find 'Duration:' in %s\n", file);
... ... @@ -307,7 +337,7 @@ float parse_ffmpeg_duration(const char * file)
return hour * 3600 + minute * 60 + sec;
}
float get_mp4_duration(const char *mediafile, bool bVideo)
float get_mp4_duration(const char *mediafile, bool bVideo, bool * bH264)
{
char buf[2048];
... ... @@ -345,20 +375,20 @@ float get_mp4_duration(const char *mediafile, bool bVideo)
sprintf(buf, "%s.txt", mediafile);
return parse_ffmpeg_duration(buf);
return parse_ffmpeg_duration(buf, bH264);
}
#ifdef WIN32
#define strcasecmp _stricmp
#endif
float get_file_duration(const char *mediafile, bool bVideo)
float get_file_duration(const char *mediafile, bool bVideo, bool * beH264 = NULL)
{
char buf[2048];
int len = strlen(mediafile);
if (len > 3) {
if (!strcasecmp(mediafile + len - 4, ".mp4")) {
return get_mp4_duration(mediafile, bVideo);
return get_mp4_duration(mediafile, bVideo, beH264);
}
}
if (bVideo){
... ... @@ -384,7 +414,8 @@ float get_file_duration(const char *mediafile, bool bVideo)
sprintf(buf, "%s.txt", mediafile);
return parse_ffmpeg_duration(buf);
float duration = parse_ffmpeg_duration(buf, beH264);
return duration;
}
void get_duration_from_video_file()
... ... @@ -394,11 +425,13 @@ void get_duration_from_video_file()
for (int i = 0; i < media_files.size(); i++){
if (media_files[i].m_type == mt_video) {
float duration = get_file_duration(media_files[i].name.c_str(), true);
bool bH264 = false;
float duration = get_file_duration(media_files[i].name.c_str(), true, &bH264);
if (duration >= 0.1) {
printf("file:%s , duration in recording file: %.3f, duration parsed from file: %.3f\n", media_files[i].name.c_str(), media_files[i].end_time - media_files[i].start_time, duration);
media_files[i].end_time = media_files[i].start_time + duration;
}
media_files[i].is_h264 = bH264;
}
}
... ... @@ -492,6 +525,7 @@ void add_media_infos()
m.t_type = tt_start;
m.duration = f.end_time - f.start_time;
m.type_time = m.start_time;
m.is_h264 = f.is_h264;
add_media_info(m);
m.t_type = tt_end;
m.type_time = m.end_time;
... ... @@ -512,10 +546,9 @@ void init()
strcpy(silence_aac_file, cfg_path);
strcat(silence_aac_file, "silence.aac");
if (only_convert_mp4 == false) {
check_audio_duration();
get_duration_from_video_file();
}
check_audio_duration();
get_duration_from_video_file();
add_media_infos();
nv = 0;
... ... @@ -570,7 +603,7 @@ int merge_audio_video(vector<media_info> & files)
sprintf(destfile, "%d.ts", nf);
merge_audio_video(audio_file, video.name.c_str(), destfile);
merge_audio_video(audio_file, video.name.c_str(), destfile, video.is_h264);
merged_files.push_back(destfile);
nf++;
... ... @@ -804,14 +837,14 @@ int process_files_to_1file(const char * output_dest_file)
else {
if (media_files.size() == 1){
fileinfo video = media_files[0];
merge_video_silence(video, silence_aac_file, "dest.ts");
merge_video_silence(video, silence_aac_file, "dest.ts", video.is_h264);
return 0;
}
for (int i = 0; i < media_files.size(); i++){
fileinfo video = media_files[i];
sprintf(destfile, "%d.ts", nf);
merge_video_silence(video, silence_aac_file, destfile);
merge_video_silence(video, silence_aac_file, destfile, video.is_h264);
merged_files.push_back(destfile);
nf++;
}
... ... @@ -983,7 +1016,7 @@ int process_files()
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, outputfile);
merge_video_silence(video, silence_aac_file, outputfile, video.is_h264);
save_out_info(video.start_time, outputfile);
}
}
... ... @@ -1018,7 +1051,7 @@ int process_only_mp4_files()
continue;
}
get_output_file_name(i, video.name.c_str(), outputfile);
convert_video_to_ts(video.name.c_str(), outputfile);
convert_video_to_ts(video.name.c_str(), outputfile, video.is_h264);
save_out_info(video.start_time, outputfile);
}
... ... @@ -1105,6 +1138,7 @@ int main(int argc, char * argv[])
printf("\n -p :only print the command,don't run ffmpeg");
printf("\n -k :keep the temp files\n");
printf("\n -c :only convert mp4 files\n");
printf("\n -t :recodec h264\n");
return -1;
}
... ... @@ -1123,6 +1157,9 @@ int main(int argc, char * argv[])
else if (!strcmp(argv[i], "-c")){
only_convert_mp4 = true;
}
else if (!strcmp(argv[i], "-t")){
recodec_h264 = true;
}
}
get_outinfo_file_name(argv[1]);
... ...