hudl / HudlFfmpeg

Hudl.Ffmpeg framework
Apache License 2.0
112 stars 32 forks source link

Video/Audio Concat #95

Open gmav opened 5 years ago

gmav commented 5 years ago

Hello i'm trying to concat two mp4 files, both contain video/audio stream. After some search i've found a working solution:

var commandFactory = CommandFactory.Create();
var command = commandFactory
.CreateOutputCommand()
.AddInput(@"c:\temp\vvv1.mp4")
.AddInput(@"c:\temp\\vvv2.mp4");

var videoConcat = command
.Select<VideoStream>(0)
.Select<VideoStream>(1)
.Filter(Filterchain.FilterTo<VideoStream>(new Concat()));

var audioConcat = command
.Select<AudioStream>(0)
.Select<AudioStream>(1)
.Filter(Filterchain.FilterTo<AudioStream>(new Concat(1, 0)));

command.Select(videoConcat, audioConcat)
.MapTo<Mp4>(@"c:\temp\output.mp4", SettingsCollection.ForOutput(new OverwriteOutput()));
commandFactory.Render();

Is there a way to do it in a single stage?

Casey-Bateman commented 5 years ago

Sorry for a late reply,

It looks like you are doing an encode concat, are the video and audio streams in the same input format? If so you can do a mux concat, basically you would add the two inputs to a txt file, then pass the txt through ffmpeg as an input with the Codec copy command, It will concatenate the two files end to end.

The input file looks like this

file 'c:\temp\vvv1.mp4'
file 'c:\temp\vvv2.mp4'

The output command, as i would recommend it, would look like this

ffmpeg -auto_convert 1 -f concat -i "c:\temp\mytextfile.txt" -c copy -shortest -movflags +faststart "c:\temp\output.mp4"

The code to do this in Hudl.Ffmpeg looks like this

            var commandFactory = CommandFactory.Create();

            var inputSettings = SettingsCollection.ForInput(
                new AutoConvert(),
                new FormatInput(FormatType.Concat));

            var outputSettings = SettingsCollection.ForOutput(
                new OverwriteOutput(),
                new MovFlags(MovFlags.EnableFastStart),
                new TrimShortest(),
                new CodecAudio(AudioCodecType.Copy),
                new CodecVideo(VideoCodecType.Copy));

            commandFactory.CreateOutputCommand()
                            .AddInput("c:\temp\mytextfile.txt", inputSettings)
                            .To<Mp4>("c:\temp\output.mp4", outputSettings);

            commandFactory.Render();
        }