Example #1
0
        public static string FixVideoFileIfNecessary()
        {
            Java.IO.File videoFile = new Java.IO.File(Path.Combine(Environment.ExternalStorageDirectory.ToString(), "video_to_fix.mp4"));

            using (IDataSource channel = new FileDataSourceImpl(videoFile.AbsolutePath)) {
                IsoFile isoFile = new IsoFile(channel);

                IList trackBoxes = isoFile.MovieBox.GetBoxes(Java.Lang.Class.FromType(typeof(TrackBox)));

                bool hasError = true;

                foreach (TrackBox trackBox in trackBoxes)
                {
                    TimeToSampleBox.Entry firstEntry = trackBox.MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox.Entries[0];

                    if (firstEntry.Delta > 10000)
                    {
                        hasError         = true;
                        firstEntry.Delta = 3000;
                    }
                }

                if (hasError)
                {
                    Movie movie = new Movie();

                    foreach (TrackBox trackBox in trackBoxes)
                    {
                        movie.AddTrack(new Mp4TrackImpl(channel.ToString() + "[" + trackBox.TrackHeaderBox.TrackId + "]", trackBox));
                    }

                    movie.Matrix = isoFile.MovieBox.MovieHeaderBox.Matrix;

                    using IContainer outt = new DefaultMp4Builder().Build(movie);

                    string filePath = Path.Combine(Environment.ExternalStorageDirectory.ToString(), "fixedVideo.mp4");

                    using FileChannel fileChannel = new RandomAccessFile(filePath, "rw").Channel;
                    outt.WriteContainer(fileChannel);

                    return(filePath);
                }
            }

            return(videoFile.AbsolutePath);
        }
        // You can merge any amount of movies, but some format are less stable
        public void Merge(string sourceDirectory, string movie1, string movie2, string destinationDirectory, string destinationFile)
        {
            var inMovies = new Com.Googlecode.Mp4parser.Authoring.Movie[] {
                MovieCreator.Build(Path.Combine(sourceDirectory, movie1)),
                MovieCreator.Build(Path.Combine(sourceDirectory, movie2)),
            };

            var videoTracks = new List <ITrack>();
            var audioTracks = new List <ITrack>();

            foreach (var m in inMovies)
            {
                foreach (var t in m.Tracks)
                {
                    if (t.Handler.Equals("soun"))
                    {
                        audioTracks.Add(t);
                    }
                    if (t.Handler.Equals("vide"))
                    {
                        videoTracks.Add(t);
                    }
                }
            }

            var result = new Com.Googlecode.Mp4parser.Authoring.Movie();

            if (audioTracks.Count > 0)
            {
                result.AddTrack(new AppendTrack(audioTracks.ToArray()));
            }
            if (videoTracks.Count > 0)
            {
                result.AddTrack(new AppendTrack(videoTracks.ToArray()));
            }

            var outContainer = new DefaultMp4Builder().Build(result);

            var fc = new FileOutputStream(Path.Combine(destinationDirectory, destinationFile)).Channel;

            outContainer.WriteContainer(fc);
            fc.Close();
        }