简体   繁体   中英

C# how to concatenate 2 MP4 files

I have 2 MP4 files that are the same frame size and same frame rate.

I want to concatenate them using C#

ANY reasonable solution is OK... I'm not limited to just using ffmpeg.

I also don't mind having to transcode one or both of the files.

Yes, I have tried using ffmpeg, using all of the various ideas from this post and others How to concatenate two MP4 files using FFmpeg?

Tried AVConv & MP4Box as well. I figured if I can find a command line option then my C# code can simply execute that command.

I WAS able to get them to concatenate AFTER doing a transcode on one input file and then used the Transport Stream intermediate file method. However, the resulting file was off by 1 frame. It was the last frame of the file and is a very important frame to my application.

I found a program, Avdshare Video Converter that does concatenate my files with zero issues. I just want to do it in my application now.

A solution using FFMPEG!

There were a few key changes needed to some of the command line examples previously posted.

Note in my question I didn't say transcoding was not an option.

I ended up transcoding both clips to be concatenated into H.264.

ffmpeg -i clip1.mp4 -vcodec libx264 -acodec aac clip1h264.mp4
ffmpeg -i clip2.mp4 -vcodec libx264 -acodec aac clip2h264.mp4

I then converted those into 2 transport streams.

ffmpeg -i clip1h264.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1.ts
ffmpeg -i clip2h264.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2.ts

The 2 transport streams were then concatenated. The key change to the success of the final concatenation was to remove the -copy option so that the command became:

ffmpeg -i "concat:temp1.ts|temp2.ts" -bsf:a aac_adtstoasc -y full.mp4

Once I had all these commands working it was a matter to using Process.Start to performs them one by one in my C# code.

Good luck to those trying to do similar operations. Hopefully this will work for you too!

A special thanks to the downvoters for all their help and encouragement. Keep doing your thing to make the programming world a better place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM