胡斌

seems ok

... ... @@ -169,31 +169,18 @@ void get_video_first_frame_jpeg(const char * video, const char * destfile)
run_shell_cmd(buf);
}
void merge_audio_pic(fileinfo audio, int nf, fileinfo video, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -loop 1 -i %s.jpg -i %d_%s -loop 0 -shortest %s %s %s", video.name.c_str(), nf, audio.name.c_str(), acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
void merge_audio_pic(fileinfo audio, int nf, const char * picfile, const char * destfile)
void merge_audio_pic(const char * audio, const char * picfile, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %d_%s -loop 0 -shortest %s %s %s", picfile, nf, audio.name.c_str(), acodec_param, vcodec_param, destfile);
sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio, acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
void merge_audio_pic(fileinfo audio, const char * picfile, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio.name.c_str(), acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
void merge_audio_pic(const char * audio, const char * picfile, const char * destfile)
void merge_audio_video(const char * audio, const char * video, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -loop 1 -i %s -i %s -loop 0 -shortest %s %s %s", picfile, audio, acodec_param, vcodec_param, destfile);
sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
... ... @@ -204,19 +191,6 @@ void merge_video_silence(fileinfo video, const char * aacfile, const char * dest
run_shell_cmd(buf);
}
void merge_audio_video(fileinfo audio, int nf, fileinfo video, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %d_%s -i %s %s %s %s", nf, audio.name.c_str(), video.name.c_str(), acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
void merge_audio_video(const char * audio, const char * video, const char * destfile)
{
char buf[2048];
sprintf(buf, "ffmpeg -y -i %s -i %s %s %s %s", audio, video, acodec_param, vcodec_param, destfile);
run_shell_cmd(buf);
}
void concate_files(vector<string > merged_files, const char * destfile)
{
... ... @@ -418,46 +392,21 @@ int merge_audio_file(vector<string> & files, const char * dest)
return 0;
}
class MergeProcess{
public:
MergeProcess();
int process_files(const char * output_dest_file);
protected:
void add_media_info(media_info m);
void add_media_infos();
void init();
int merge_audio_video(vector<media_info> & files);
int merge_audio_pic(vector<media_info> & files);
int find_video_between_the_audio();
int find_video_end();
bool is_audio_start(int index);
int split_audio(int index_from, vector<media_info> & result);
void get_front_info(int index_to, vector<media_info> &cur_processing);
int process_va();
protected:
list <media_info> sorted_media;
deque<media_info> sorted_infos;
vector<media_info> media_infos;
vector<string > merged_files;
vector<string> tmp_files;
int nv; // the index of processing video file
int nf;//the index of processing target merged ts
char destfile[1024], audio_file[1024], pic_file[1024];
char blank_pic_file[1024];
char silence_aac_file[1024];
int audio_index;
int audio_start;
fileinfo audio;
fileinfo video;
};
MergeProcess::MergeProcess(){
init();
}
list <media_info> sorted_media;
deque<media_info> sorted_infos;
vector<media_info> media_infos;
vector<string > merged_files;
vector<string> tmp_files;
int nv; // the index of processing video file
int nf;//the index of processing target merged ts
char destfile[1024], audio_file[1024], pic_file[1024];
char blank_pic_file[1024];
char silence_aac_file[1024];
void MergeProcess::add_media_info(media_info m)
void add_media_info(media_info m)
{
list<media_info>::iterator it = sorted_media.begin();
for (; it != sorted_media.end(); it++){
... ... @@ -468,7 +417,7 @@ void MergeProcess::add_media_info(media_info m)
sorted_media.insert(it, m);
}
void MergeProcess::add_media_infos()
void add_media_infos()
{
for (int i = 0; i < media_files.size(); i++) {
fileinfo f = media_files[i];
... ... @@ -493,7 +442,7 @@ void MergeProcess::add_media_infos()
}
}
void MergeProcess::init()
void init()
{
strcpy(blank_pic_file, cfg_path);
strcat(blank_pic_file, "blank.jpg");
... ... @@ -507,13 +456,12 @@ void MergeProcess::init()
nv = 0;
nf = 0;
audio_index = 0;
}
int MergeProcess::merge_audio_video(vector<media_info> & files)
int merge_audio_video(vector<media_info> & files)
{
vector<std::string > merge_audio_files;
vector<string> merge_audio_files;
int nsilence = 0;
media_info video = files[0];
... ... @@ -524,14 +472,14 @@ int MergeProcess::merge_audio_video(vector<media_info> & files)
media_info audio_end = files[i + 1];
if (audio.type_time - start_time > 0.1){
sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence
::split_audio(silence_aac_file, 0, audio.type_time - start_time, audio_file);
split_audio(silence_aac_file, 0, audio.type_time - start_time, audio_file);
merge_audio_files.push_back(silence_aac_file);
tmp_files.push_back(silence_aac_file);
}
if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio.type_time > 0.10) {
sprintf(audio_file, "%d_%s", nf, audio.name.c_str());
::split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file);
split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file);
tmp_files.push_back(audio_file);
}
... ... @@ -545,7 +493,7 @@ int MergeProcess::merge_audio_video(vector<media_info> & files)
if (i == files.size() - 2){
if (video.end_time - audio_end.type_time > 0.1){
sprintf(audio_file, "%d_%d_silence.aac", nf, nsilence++);//a duration of silence
::split_audio(silence_aac_file, 0, video.end_time - audio_end.type_time, audio_file);
split_audio(silence_aac_file, 0, video.end_time - audio_end.type_time, audio_file);
merge_audio_files.push_back(silence_aac_file);
tmp_files.push_back(silence_aac_file);
}
... ... @@ -557,21 +505,21 @@ int MergeProcess::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);
merged_files.push_back(destfile);
nf++;
return 0;
}
int MergeProcess::merge_audio_pic(vector<media_info> & files)
int merge_audio_pic(vector<media_info> & files)
{
media_info audio = files[0];
media_info audio_end = files[1];
if (audio.type_time - audio.start_time > 0.10 || audio_end.end_time - audio.type_time > 0.10) {
sprintf(audio_file, "%d_%s", nf, audio.name.c_str());
::split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file);
split_audio(audio.name.c_str(), audio.type_time - audio.start_time, audio_end.type_time - audio.start_time, audio_file);
tmp_files.push_back(audio_file);
}
else{
... ... @@ -594,14 +542,14 @@ int MergeProcess::merge_audio_pic(vector<media_info> & files)
strcpy(pic_file, blank_pic_file);
}
::merge_audio_pic(audio_file, pic_file, destfile);
merge_audio_pic(audio_file, pic_file, destfile);
merged_files.push_back(destfile);
nf++;
return 0;
}
int MergeProcess::find_video_between_the_audio()
int find_video_between_the_audio()
{
int index = sorted_infos[0].index;
int video_index = 0;
... ... @@ -617,7 +565,7 @@ int MergeProcess::find_video_between_the_audio()
return video_index;
}
int MergeProcess::find_video_end()
int find_video_end()
{
int index = sorted_infos[0].index;
int video_index = 0;
... ... @@ -630,30 +578,68 @@ int MergeProcess::find_video_end()
return video_index;
}
bool MergeProcess::is_audio_start(int index)
bool is_audio_start(int index)
{
return (sorted_infos[index].m_type == mt_audio && sorted_infos[index].t_type == tt_start);
}
int MergeProcess::split_audio(int index_from, vector<media_info> & result)
int split_audio_for_video(int audio_start,vector<media_info> & result)
{
media_info audio = sorted_infos[audio_start];
media_info video = sorted_infos[audio_start + 1];
result.clear();
for (int i = 0; i <= audio_start; i++){
result.push_back(sorted_infos[i]);
}
if (sorted_infos[audio_start + 2].index == sorted_infos[audio_start].index){
if (sorted_infos[audio_start + 2].type_time - sorted_infos[audio_start + 1].type_time < 0.1){//no need to split
result.push_back(sorted_infos[audio_start + 2]);//put the audio end to the result
result.push_back(video);//push the video end to the result
for (int i = 0; i <= audio_start + 2; i++){ //remove the infos including the audio end
sorted_infos.pop_front();
}
return 0;
}
}
audio.t_type = tt_end;
audio.type_time = video.type_time;
result.push_back(audio);
result.push_back(video);
for (int i = 0; i <= audio_start +1; i++){ //remove the infos including the video end
sorted_infos.pop_front();
}
audio.t_type = tt_start;
sorted_infos.push_front(audio);
return 0;
}
int split_audio_for_pic(vector<media_info> & result)
{
media_info audio = sorted_infos[index_from];
media_info video = sorted_infos[index_from + 1];
media_info audio = sorted_infos[0];
media_info video = sorted_infos[1];
result.clear();
for (int i = 0; i < index_from; i++){
for (int i = 0; i < 1; i++){
result.push_back(sorted_infos[i]);
}
if (sorted_infos[index_from + 2].index == sorted_infos[index_from].index){
if (sorted_infos[index_from].end_time - sorted_infos[index_from + 1].start_time < 0.1){//no need to split
result.push_back(sorted_infos[index_from + 2]);//put the audio end to the result
for (int i = 0; i < index_from + 2; i++){
if (sorted_infos[2].index == sorted_infos[0].index){
if (sorted_infos[2].type_time - sorted_infos[1].type_time < 0.1){//no need to split
result.push_back(sorted_infos[2]);//put the audio end to the result
for (int i = 0; i < 3; i++){
sorted_infos.pop_front();
}
sorted_infos.push_front(video);
sorted_infos.push_front(video);
return 0;
}
}
... ... @@ -662,7 +648,7 @@ int MergeProcess::split_audio(int index_from, vector<media_info> & result)
audio.type_time = video.start_time;
result.push_back(audio);
for (int i = 0; i < index_from + 1; i++){
for (int i = 0; i < 2; i++){
sorted_infos.pop_front();
}
... ... @@ -672,7 +658,7 @@ int MergeProcess::split_audio(int index_from, vector<media_info> & result)
return 0;
}
void MergeProcess::get_front_info(int index_to, vector<media_info> &cur_processing)
void get_front_info(int index_to, vector<media_info> &cur_processing)
{
cur_processing.clear();
for (int i = 0; i <= index_to; i++){
... ... @@ -683,7 +669,7 @@ void MergeProcess::get_front_info(int index_to, vector<media_info> &cur_processi
}
}
int MergeProcess::process_va()
int process_va()
{
vector<media_info> cur_processing;
while (sorted_infos.size())
... ... @@ -693,7 +679,7 @@ int MergeProcess::process_va()
int index = find_video_between_the_audio();
if (index > 0) //have_video
{
split_audio(index, cur_processing);
split_audio_for_pic(cur_processing);
}
else {
get_front_info(1, cur_processing);
... ... @@ -703,7 +689,7 @@ int MergeProcess::process_va()
else{
int index = find_video_end();
if (is_audio_start(index - 1)) {
split_audio(index - 1, cur_processing);
split_audio_for_video(index - 1, cur_processing);
}
else {
get_front_info(index, cur_processing);
... ... @@ -715,14 +701,15 @@ int MergeProcess::process_va()
}
int MergeProcess::process_files(const char * output_dest_file)
int process_files(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
init();
if (!media_files.size()){
return 0;
}
// judge if it is only one type
media_type mt = media_files[0].m_type;
bool only_one_type = true;
... ... @@ -737,14 +724,14 @@ int MergeProcess::process_files(const char * output_dest_file)
if (mt == mt_audio) {
if (media_files.size() == 1){
fileinfo audio = media_files[0];
::merge_audio_pic(audio, blank_pic_file, "dest.ts");
merge_audio_pic(audio.name.c_str(), blank_pic_file, "dest.ts");
return 0;
}
for (int i = 0; i < media_files.size(); i++){
fileinfo audio = media_files[i];
sprintf(destfile, "%d.ts", nf);
::merge_audio_pic(audio, blank_pic_file, destfile);
merge_audio_pic(audio.name.c_str(), blank_pic_file, destfile);
merged_files.push_back(destfile);
nf++;
}
... ... @@ -867,8 +854,7 @@ int main(int argc, char * argv[])
load_codec_param();
MergeProcess mp;
mp.process_files("dest.ts");
process_files("dest.ts");
return 0;
}
... ...