예제 #1
0
    void Start()
    {
        videoNames = new List <string>();
        string path = GetPath(folderName);
        var    info = new DirectoryInfo(path);

        FileInfo[] fileInfos = info.GetFiles();
        for (int i = 0; i < fileInfos.Length; ++i)
        {
            FileInfo fileInfo = fileInfos[i];
            string[] infos    = fileInfo.Name.Split('.');
            if (infos[infos.Length - 1] != "meta")
            {
                videoNames.Add(fileInfo.Name);
            }
        }

        mediaDecoder = GetComponent <MediaDecoder>();
        mediaDecoder.initDecoder(System.IO.Path.Combine(GetPath(folderName), videoNames[current]));
        mediaDecoder.onInitComplete.AddListener(mediaDecoder.startDecoding);
        mediaDecoder.onVideoEnd.AddListener(Rewind);

        videoTexture         = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
        videoTexturePrevious = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
    }
예제 #2
0
        protected override void ProcessOutputBuffer(MediaCodec.BufferInfo bufferInfo, int outputIndex)
        {
            MediaDecoder.ReleaseOutputBuffer(outputIndex, true);

            isSeekRequestCompleted = true;

            VideoInstance.OnReceiveNotificationToUpdateVideoTextureSurface();
        }
예제 #3
0
 private void MediaDecoder_OnInstantiated(MediaDecoder instance)
 {
     log.Info("Media decoder instantiated");
     MediaDecoder.OnInstantiated -= MediaDecoder_OnInstantiated;
     instance.OnTimeUpdate       += HandleTimeUpdate;
     instance.OnNewFileIsPlaying += HandleNewFileIsPlaying;
     instance.OnStop             += HandleStop;
 }
예제 #4
0
    public InputSource(string url)
    {
        GameObject o           = new GameObject("[Stream]" + url);
        Type       decoderType = FindDecoderType(url);

        mediaDecoder           = (MediaDecoder)o.AddComponent(decoderType);
        mediaDecoder.mediaPath = url;
    }
예제 #5
0
        FileStream fsStream;            // For Debugging
        #endregion

        #region Initialize
        public MediaStreamer(MediaRouter player)
        {
            this.player = player;

            decoder = new MediaDecoder(player, player.verbosity);
            decoder.isForBuffering = true;
            decoder.HWAccel        = false;
            decoder.Threads        = 1;
            decoder.BufferingDone  = BufferingDone;
        }
        /// <summary>
        /// decode video to image
        /// filter graph:
        /// ┌──────┐     ┌──────┐     ┌─────┐     ┌──────────┐     ┌──────┐
        /// │input0│---->│buffer│---->│scale│---->│buffersink│---->│output│
        /// └──────┘     └──────┘     └─────┘     └──────────┘     └──────┘
        /// </summary>
        /// <param name="inputFile">input video file</param>
        /// <param name="outDirectory">folder for output image files</param>
        /// <param name="scaleOptions">scale options see http://ffmpeg.org/ffmpeg-filters.html#scale-1 </param>
        public DecodeVideoWithCustomCodecScaledToMat(string inputFile, string outDirectory, string scaleOptions = "512:288")
        {
            using (MediaReader reader = new MediaReader(inputFile, null, null))
            {
                var videoIndex = reader.First(_ => _.Codec.AVCodecContext.codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO).Index;

                unsafe
                {
                    // relpace the default vide decode
                    // !!! IMPORTANT NOTE: This sample won't work, if you haven't downloaded ffmpeg (GPL license, as it is more complete), and you don't have NVIDIA hardware (CUDA) !!!
                    reader[videoIndex].Codec = MediaDecoder.CreateDecoder("h264_cuvid", _ => ffmpeg.avcodec_parameters_to_context(_, reader[videoIndex].Stream.codecpar));
                }

                int        height              = reader[videoIndex].Codec.AVCodecContext.height;
                int        width               = reader[videoIndex].Codec.AVCodecContext.width;
                int        format              = (int)reader[videoIndex].Codec.AVCodecContext.pix_fmt;
                AVRational time_base           = reader[videoIndex].TimeBase;
                AVRational sample_aspect_ratio = reader[videoIndex].Codec.AVCodecContext.sample_aspect_ratio;

                /* We are moving the packet to CUDA to perform the scaling.
                 * We can then:
                 * - remove hwdownload and format to leave it in CUDA, and forward the pointer to any other function, or write the frame to the output video
                 * - convert it to MAT whereas converting speed depends on the size of the scaled frame.
                 */
                MediaFilterGraph filterGraph = new MediaFilterGraph();
                filterGraph.AddVideoSrcFilter(new MediaFilter(MediaFilter.VideoSources.Buffer), width, height, (AVPixelFormat)format, time_base, sample_aspect_ratio)
                .LinkTo(0, filterGraph.AddFilter(new MediaFilter("scale"), scaleOptions))
                .LinkTo(0, filterGraph.AddVideoSinkFilter(new MediaFilter(MediaFilter.VideoSinks.Buffersink)));
                filterGraph.Initialize();

                var sw = Stopwatch.StartNew();
                foreach (var packet in reader.ReadPacket())
                {
                    foreach (var frame in reader[videoIndex].ReadFrame(packet))
                    {
                        filterGraph.Inputs.First().WriteFrame(frame);
                        foreach (var filterFrame in filterGraph.Outputs.First().ReadFrame())
                        {
                            using (var image = filterFrame.ToMat())
                            {
                                image.Save(Path.Combine(Directory.CreateDirectory(outDirectory).FullName, $"{DateTime.Now.Ticks}.jpg"));
                            }
                        }
                    }
                }
                Console.WriteLine($"Converting to MAT [ processed in {sw.Elapsed.TotalMilliseconds:0} ms ]");
            }
        }
예제 #7
0
        protected override void ProcessOutputBuffer(MediaCodec.BufferInfo bufferInfo, int outputIndex)
        {
            var buffer = MediaDecoder.GetOutputBuffer(outputIndex);

            if (audioChunckBuffer == null || audioChunckBuffer.Length < bufferInfo.Size)
            {
                //Create a reusable byte buffer with some extra marging
                audioChunckBuffer = new byte[bufferInfo.Size + 512];
            }

            buffer.Get(audioChunckBuffer, 0, bufferInfo.Size); // Read the buffer all at once
            buffer.Clear();                                    // Must do, otherwise next time we receive this outputbuffer it might be bad

            Logger.Debug("Audio chunck" + " time:" + bufferInfo.PresentationTimeUs + " size: " + bufferInfo.Size);

            MediaDecoder.ReleaseOutputBuffer(outputIndex, false);

            isSeekRequestCompleted = true;
        }