Exemple #1
0
        public void Execute()
        {
            /*
             * Reads the values of the video (width, heigth, fps...) and stores them
             * into file_values.
             *
             * Returns (False,AUDIO) if the file is not a video (with AUDIO the number
             * of audio tracks)
             *
             * Returns (True,0) if the file is a right video file
             */

            BackendPrograms mplayerLocation = new BackendPrograms(mplayerPath);

            int audio   = 0;
            int video   = 0;
            int nframes = 1;

            int minimum_audio = 10000;

            _AudioList    = new List <int> ();
            _AudioTracks  = new List <AudioTrackInfo> ();
            _SubtitleList = new List <SubtitlesInfo> ();
            // if CHECK_AUDIO is TRUE, we just check if it's an audio file

            //if check_audio:
            //    nframes=0
            //else:
            //    nframes=1

            using (var handle = new System.Diagnostics.Process()) {
                handle.StartInfo.UseShellExecute        = false;
                handle.StartInfo.CreateNoWindow         = true;
                handle.StartInfo.RedirectStandardOutput = true;
                handle.StartInfo.RedirectStandardError  = true;

                handle.StartInfo.FileName  = mplayerLocation.MPlayer;
                handle.StartInfo.Arguments = string.Format("-loop 1 -identify -ao null -vo null -frames 0 {0} \"{1}\"", nframes.ToString(), filePath);
                handle.Start();
                string       line      = "";
                StringReader strReader = new StringReader(handle.StandardOutput.ReadToEnd());

                while ((line = strReader.ReadLine()) != null)                   //while (handle.HasExited == false)

                {
                    if (line.Trim() == "")
                    {
                        continue;
                    }
                    int position = line.IndexOf("ID_");
                    if (position == -1)
                    {
                        continue;
                    }
                    line = line.Substring(position);
                    if (line.StartsWith("ID_VIDEO_BITRATE"))
                    {
                        _VideoBitrate = Globals.IntParse(line.Substring(17)) / 1000;   // kilobits per second
                    }
                    else if (line.StartsWith("ID_VIDEO_WIDTH"))
                    {
                        _Width = Globals.IntParse(line.Substring(15));
                    }
                    else if (line.StartsWith("ID_VIDEO_HEIGHT"))
                    {
                        _Height = Globals.IntParse(line.Substring(16));
                    }
                    else if (line.StartsWith("ID_VIDEO_ASPECT"))
                    {
                        _AspectRatio = Globals.FloatParse(line.Substring(16));
                    }
                    else if (line.StartsWith("ID_VIDEO_FPS"))
                    {
                        _fps = (int)Globals.FloatParse(line.Substring(13));
                    }
                    else if (line.StartsWith("ID_AUDIO_BITRATE"))
                    {
                        _AudioBitrate = Globals.IntParse(line.Substring(17)) / 1000;   // kilobits per second
                    }
                    else if (line.StartsWith("ID_AUDIO_RATE"))
                    {
                        _AudioRate = Globals.IntParse(line.Substring(14));
                    }
                    else if (line.StartsWith("ID_LENGTH"))
                    {
                        _Length = (int)Globals.FloatParse(line.Substring(10));
                    }
                    else if (line.StartsWith("ID_VIDEO_ID"))
                    {
                        video += 1;
                        _Video = true;
                    }
                    else if (line.StartsWith("ID_AUDIO_ID"))
                    {
                        audio += 1;
                        _Audio = true;
                        int audio_track = Globals.IntParse(line.Substring(12));
                        if (minimum_audio > audio_track)
                        {
                            minimum_audio = audio_track;
                        }
                        _AudioList.Add(audio_track);

                        AudioTrackInfo info = new AudioTrackInfo();
                        info.ID = audio_track;
                        _AudioTracks.Add(info);
                    }
                    else if (line.StartsWith("ID_AID_") && line.Substring(9, 4) == "LANG")
                    {
                        if (_AudioTracks.Count > 0)
                        {
                            string value = line.Substring(14);

                            _AudioTracks [_AudioTracks.Count - 1].Language = value;
                        }
                    }
                    else if (line.StartsWith("ID_AID_") && line.Substring(9, 4) == "NAME")
                    {
                        if (_AudioTracks.Count > 0)
                        {
                            string value = line.Substring(14);

                            _AudioTracks [_AudioTracks.Count - 1].Name = value;
                        }
                    }
                    else if (line.StartsWith("ID_SUBTITLE_ID"))
                    {
                        int value = Globals.IntParse(line.Substring(15));

                        SubtitlesInfo info = new SubtitlesInfo();
                        info.ID = value;
                        _SubtitleList.Add(info);
                    }
                    else if (line.StartsWith("ID_SID_") && line.Substring(9, 4) == "LANG")
                    {
                        if (_SubtitleList.Count > 0)
                        {
                            string value = line.Substring(14);

                            _SubtitleList [_SubtitleList.Count - 1].Language = value;
                        }
                    }
                    else if (line.StartsWith("ID_SID_") && line.Substring(9, 4) == "NAME")
                    {
                        if (_SubtitleList.Count > 0)
                        {
                            string value = line.Substring(14);

                            _SubtitleList [_SubtitleList.Count - 1].Name = value;
                        }
                    }
                }

                handle.WaitForExit();
                handle.Close();
            }
            if (_AspectRatio == 0.0)
            {
                _AspectRatio = ((float)_Width / (float)_Height);
                if (_AspectRatio <= 1.5)
                {
                    _AspectRatio = (ScreenAspectRatio.FourThree);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="mplayerPath">If mplayerPath is left empty it will search for mplayer.exe in 
        /// "current directory\backend\mplayer.exe" on windows and mplayer in the path on linux.</param>
        public Discover(string filePath, string mplayerPath)
        {
            /*
             Reads the values of the video (width, heigth, fps...) and stores them
             into file_values.

             Returns (False,AUDIO) if the file is not a video (with AUDIO the number
             of audio tracks)

             Returns (True,0) if the file is a right video file
             */

            BackendPrograms mplayerLocation = new BackendPrograms(mplayerPath);

            int audio = 0;
            int video = 0;
            int nframes = 1;

            int minimum_audio = 10000;
            _AudioList = new List<int>();
            _AudioTracks = new List<AudioTrackInfo>();
            _SubtitleList = new List<SubtitlesInfo>();
            // if CHECK_AUDIO is TRUE, we just check if it's an audio file

            //if check_audio:
            //    nframes=0
            //else:
            //    nframes=1

            System.Diagnostics.Process handle = new System.Diagnostics.Process();

            handle.StartInfo.UseShellExecute = false;
            handle.StartInfo.CreateNoWindow = true;
            handle.StartInfo.RedirectStandardOutput = true;
            handle.StartInfo.RedirectStandardError = true;

            handle.StartInfo.FileName = mplayerLocation.MPlayer;
            handle.StartInfo.Arguments = string.Format("-loop 1 -identify -ao null -vo null -frames 0 {0} \"{1}\"", nframes.ToString(), filePath);
            handle.Start();
            string line = "";
            StringReader strReader = new StringReader(handle.StandardOutput.ReadToEnd());

            while ((line = strReader.ReadLine()) != null)
            //while (handle.HasExited == false)
            {

                if (line.Trim() == "")
                {
                    continue;
                }
                int position = line.IndexOf("ID_");
                if (position == -1)
                {
                    continue;
                }
                line = line.Substring(position);
                if (line.StartsWith("ID_VIDEO_BITRATE"))
                {
                    _VideoBitrate = Globals.IntParse(line.Substring(17)) / 1000; // kilobits per second
                }
                else if (line.StartsWith("ID_VIDEO_WIDTH"))
                {
                    _Width = Globals.IntParse(line.Substring(15));
                }
                else if (line.StartsWith("ID_VIDEO_HEIGHT"))
                {
                    _Height = Globals.IntParse(line.Substring(16));
                }
                else if (line.StartsWith("ID_VIDEO_ASPECT"))
                {
                    _AspectRatio = Globals.FloatParse(line.Substring(16));
                }
                else if (line.StartsWith("ID_VIDEO_FPS"))
                {
                    _fps = (int)Globals.FloatParse(line.Substring(13));
                }
                else if (line.StartsWith("ID_AUDIO_BITRATE"))
                {
                    _AudioBitrate = Globals.IntParse(line.Substring(17)) / 1000; // kilobits per second
                }
                else if (line.StartsWith("ID_AUDIO_RATE"))
                {
                    _AudioRate = Globals.IntParse(line.Substring(14));
                }
                else if (line.StartsWith("ID_LENGTH"))
                {
                    _Length = (int)Globals.FloatParse(line.Substring(10));
                }
                else if (line.StartsWith("ID_VIDEO_ID"))
                {
                    video += 1;
                    _Video = true;
                }
                else if (line.StartsWith("ID_AUDIO_ID"))
                {
                    audio += 1;
                    _Audio = true;
                    int audio_track = Globals.IntParse(line.Substring(12));
                    if (minimum_audio > audio_track)
                    {
                        minimum_audio = audio_track;
                    }
                    _AudioList.Add(audio_track);

                    AudioTrackInfo info = new AudioTrackInfo();
                    info.ID = audio_track;
                    _AudioTracks.Add(info);

                }
                else if (line.StartsWith("ID_AID_") && line.Substring(9, 4) == "LANG")
                {
                    if (_AudioTracks.Count > 0)
                    {
                        string value = line.Substring(14);

                        _AudioTracks[_AudioTracks.Count - 1].Language = value;
                    }
                }
                else if (line.StartsWith("ID_AID_") && line.Substring(9, 4) == "NAME")
                {
                    if (_AudioTracks.Count > 0)
                    {
                        string value = line.Substring(14);

                        _AudioTracks[_AudioTracks.Count-1].Name = value;
                    }
                }
                else if (line.StartsWith("ID_SUBTITLE_ID"))
                {
                    int value = Globals.IntParse(line.Substring(15));

                    SubtitlesInfo info = new SubtitlesInfo();
                    info.ID = value;
                    _SubtitleList.Add(info);

                }
                else if (line.StartsWith("ID_SID_") && line.Substring(9, 4) == "LANG")
                {
                    if (_SubtitleList.Count > 0)
                    {
                        string value = line.Substring(14);

                        _SubtitleList[_SubtitleList.Count - 1].Language = value;
                    }
                }
                else if (line.StartsWith("ID_SID_") && line.Substring(9, 4) == "NAME")
                {
                    if (_SubtitleList.Count > 0)
                    {
                        string value = line.Substring(14);

                        _SubtitleList[_SubtitleList.Count - 1].Name = value;
                    }
                }
            }

            handle.WaitForExit();
            handle.Close();

            if (_AspectRatio == 0.0)
            {
                _AspectRatio = ((float)_Width / (float)_Height);
                if (_AspectRatio <= 1.5)
                {
                    _AspectRatio = (ScreenAspectRatio.FourThree);
                }
            }
        }
        public void Execute()
        {
            // As work around can run mpv commandline and parse output
            // mpv video_name.mp4 --vo null -ao null --frames 1 -v



            /*
             * Reads the values of the video (width, heigth, fps...) and stores them
             * into file_values.
             *
             * Returns (False,AUDIO) if the file is not a video (with AUDIO the number
             * of audio tracks)
             *
             * Returns (True,0) if the file is a right video file
             */
            _mpv = new Mpv(libmpvPath);

            // Must be set before initializeation
            _mpv.SetOption("frames", MpvFormat.MPV_FORMAT_INT64, 148);

            _mpv.Initialize();

            _mpv.SetOption("wid", MpvFormat.MPV_FORMAT_INT64, -1);
            _mpv.SetOption("vo", MpvFormat.MPV_FORMAT_STRING, "null");
            _mpv.SetOption("ao", MpvFormat.MPV_FORMAT_STRING, "null");

            _mpv.DoMpvCommand("loadfile", filePath);

            // HACK: wait for video to load
            System.Threading.Thread.Sleep(1000);
            //_mpv.SetProperty ("pause", MpvFormat.MPV_FORMAT_STRING, "yes");

            _Width       = _mpv.GetPropertyInt("width");
            _Height      = _mpv.GetPropertyInt("height");
            _AspectRatio = _mpv.GetPropertyFloat("video-aspect");

            int bits = _mpv.GetPropertyInt("audio-bitrate");

            //int bytes = Bits2Bytes (bits);
            //int kb = Bytes2Kilobytes (bytes);
            //_AudioBitrate = (int)Math.Round (bits / 1024m, 0);
            _AudioBitrate = bits;
            _AudioRate    = _mpv.GetPropertyInt("audio-params/samplerate");
            _Length       = _mpv.GetPropertyInt("duration");

            //_fps = _mpv.GetPropertyInt ("container-fps");
            _fps = _mpv.GetPropertyInt("fps");
            _mpv.TryGetPropertyInt("video-bitrate", out _VideoBitrate);

            string videoFormat = _mpv.GetProperty("video-format");

            if (!string.IsNullOrWhiteSpace(videoFormat))
            {
                _Video = true;
            }


            _AudioList    = new List <int> ();
            _AudioTracks  = new List <AudioTrackInfo> ();
            _SubtitleList = new List <SubtitlesInfo> ();

            int trackCount = _mpv.GetPropertyInt("track-list/count");

            foreach (int i in Enumerable.Range(0, trackCount))
            {
                string trackType = _mpv.GetProperty($"track-list/{i}/type");
                int    trackId   = _mpv.GetPropertyInt($"track-list/{i}/id");
                string name;

                _mpv.TryGetProperty($"track-list/{i}/title", out name);
                string language;
                _mpv.TryGetProperty($"track-list/{i}/lang", out language);

                if (trackType == "audio")
                {
                    _AudioList.Add(trackId);

                    var info = new AudioTrackInfo()
                    {
                        ID       = trackId,
                        Name     = name,
                        Language = language
                    };

                    _AudioTracks.Add(info);
                }
                else if (trackType == "sub")
                {
                    var info = new SubtitlesInfo()
                    {
                        ID       = trackId,
                        Name     = name,
                        Language = language
                    };

                    _SubtitleList.Add(info);
                }
            }


            if (_AspectRatio == 0.0)
            {
                _AspectRatio = ((float)_Width / (float)_Height);
                if (_AspectRatio <= 1.5)
                {
                    _AspectRatio = (ScreenAspectRatio.FourThree);
                }
            }
        }