Example #1
0
        static int Main(string[] args)
        {
            Library.Initialize();

            // Set license information. To run AVBlocks in demo mode, comment the next line out
            // Library.SetLicense("<license-string>");

            string    inputFile          = Path.Combine(ExeDir, @"..\assets\mov\big_buck_bunny_trailer_iphone.m4v");
            string    outputFile         = "overlay_yuv_jpegs.mp4";
            const int imageOverlayFrames = 250;
            string    imageOverlayFiles  = Path.Combine(ExeDir, @"..\assets\overlay\cube\cube{0:d4} (128x96).jpg");

            VideoStreamInfo uncompressedVideo = null;

            using (var info = new MediaInfo())
            {
                info.Inputs[0].File = inputFile;

                if (!info.Open())
                {
                    PrintError("load info", info.Error);
                    return((int)ExitCodes.Error);
                }

                foreach (var socket in info.Outputs)
                {
                    foreach (var pin in socket.Pins)
                    {
                        StreamInfo si = pin.StreamInfo;
                        if (si.MediaType == MediaType.Video)
                        {
                            uncompressedVideo = si.Clone() as VideoStreamInfo;
                            break;
                        }
                    }
                }
            }

            uncompressedVideo.StreamType  = StreamType.UncompressedVideo;
            uncompressedVideo.ColorFormat = ColorFormat.YUV420;

            var outputVideo = (VideoStreamInfo)uncompressedVideo.Clone();

            outputVideo.StreamType = StreamType.H264;

            try { System.IO.File.Delete(outputFile); }
            catch { }

            int decodedSamples   = 0;
            int overlayedSamples = 0;
            int encodedSamples   = 0;

            using (var overlay = new Overlay())
                using (var decoder = CreateDecoder(inputFile))
                    using (var encoder = CreateEncoder(uncompressedVideo, outputVideo, outputFile))
                    {
                        if (!decoder.Open())
                        {
                            PrintError("decoder open", decoder.Error);
                            return((int)ExitCodes.DecoderError);
                        }

                        if (!encoder.Open())
                        {
                            PrintError("encoder open", encoder.Error);
                            return((int)ExitCodes.EncoderError);
                        }

                        int outputIndex;
                        var decodedSample   = new MediaSample();
                        var overlayedSample = new MediaSample();

                        while (true)
                        {
                            if (!decoder.Pull(out outputIndex, decodedSample))
                            {
                                PrintError("decoder pull", decoder.Error);
                                break;
                            }
                            ++decodedSamples;

                            var imageOverlayFile = string.Format(imageOverlayFiles, overlayedSamples % imageOverlayFrames);

                            overlay.Close();
                            if (!overlay.Open(imageOverlayFile, StreamType.Jpeg, uncompressedVideo))
                            {
                                PrintError("overlay open", overlay.Error);
                                break;
                            }

                            if (!overlay.Push(0, decodedSample))
                            {
                                PrintError("overlay push", overlay.Error);
                                break;
                            }

                            if (!overlay.Pull(out outputIndex, overlayedSample))
                            {
                                PrintError("overlay pull", overlay.Error);
                                break;
                            }
                            ++overlayedSamples;


                            if (!encoder.Push(0, overlayedSample))
                            {
                                PrintError("encoder push", encoder.Error);
                                break;
                            }
                            ++encodedSamples;
                        }
                        ;

                        decoder.Close();
                        overlay.Close();
                        encoder.Flush();
                        encoder.Close();
                    }

            Console.WriteLine("samples decoded/overlayed/encoded: {0}/{1}/{2}",
                              decodedSamples, overlayedSamples, encodedSamples);

            bool success = (decodedSamples > 0 && decodedSamples == encodedSamples);

            if (success)
            {
                Console.WriteLine("output file: {0}", Path.GetFullPath(outputFile));
            }

            Library.Shutdown();

            return(success ? (int)ExitCodes.Success : (int)ExitCodes.Error);
        }
Example #2
0
        static bool ReEncode(Options opt)
        {
            if (File.Exists(opt.OutputFile))
            {
                File.Delete(opt.OutputFile);
            }

            using (var transcoder = new Transcoder())
            {
                // In order to use the production release for testing (without a valid license),
                // the transcoder demo mode must be enabled.
                transcoder.AllowDemoMode = true;

                using (var mediaInfo = new MediaInfo())
                {
                    mediaInfo.Inputs[0].File = opt.InputFile;

                    if (!mediaInfo.Open())
                    {
                        PrintError("Open MediaInfo", mediaInfo.Error);
                        return(false);
                    }

                    // Add Inputs
                    {
                        var socket = MediaSocket.FromMediaInfo(mediaInfo);
                        transcoder.Inputs.Add(socket);
                    }
                }

                // Add Outputs
                {
                    // Create output socket
                    var socket   = new MediaSocket();
                    var inSocket = transcoder.Inputs[0];

                    socket.StreamType = inSocket.StreamType;
                    socket.File       = opt.OutputFile;

                    // Add pins with ReEncode parameter set to Use.On
                    foreach (var inPin in inSocket.Pins)
                    {
                        StreamInfo si  = (StreamInfo)inPin.StreamInfo.Clone();
                        var        pin = new MediaPin();
                        pin.StreamInfo = (StreamInfo)si.Clone();

                        if ((MediaType.Video == si.MediaType) && opt.ReEncodeVideo)
                        {
                            pin.Params.Add(Param.ReEncode, Use.On);
                        }

                        if ((MediaType.Audio == si.MediaType) && opt.ReEncodeAudio)
                        {
                            pin.Params.Add(Param.ReEncode, Use.On);
                        }

                        socket.Pins.Add(pin);
                    }

                    transcoder.Outputs.Add(socket);
                }


                bool result = transcoder.Open();
                PrintError("Open Transcoder", transcoder.Error);
                if (!result)
                {
                    return(false);
                }

                result = transcoder.Run();
                PrintError("Run Transcoder", transcoder.Error);
                if (!result)
                {
                    return(false);
                }

                transcoder.Close();
            }

            return(true);
        }