private void Parse(string url)
        {
            if (!isPrepared)
            {
                Debug.LogWarningFormat(LOG_FORMAT, "Youtube-dl is not ready for parsing yet!");
                return;
            }
            status = ProcessingStatus.PARSING;

            options = ValidParseOptions(options);

            if (Application.platform == RuntimePlatform.Android)
            {
                #region ANDROID PARSE SECTION

                if (AndroidJNI.AttachCurrentThread() != 0)
                {
                    EnqueueError(new ErrorEvent(ErrorCode.ANDROID_PLUGIN_ERROR, "Android attach current thread failed!"));
                    return;
                }

                string    output = androidPluginInstance.Call <string>("getVideoInfo", url, options);
                MediaInfo info   = JsonUtility.FromJson <MediaInfo>(output);
                if (!string.IsNullOrEmpty(info.error))
                {
                    EnqueueError(new ErrorEvent(ErrorCode.PARSE_FAILED, info.error));
                    return;
                }
                lock (parseCompletedQueue) parseCompletedQueue.Enqueue(info);

                // done
                status = ProcessingStatus.READY;

                AndroidJNI.DetachCurrentThread();

                #endregion
            }
            else
            {
                #region STANDALONE & EDITOR PARSE SECTION

                // parse video url
                try
                {
                    string arguments = string.Format(" {0} --dump-json {1}", options, url);

                    Process process = new Process();
                    process.StartInfo.FileName  = YTDLUtils.path;
                    process.StartInfo.Arguments = arguments;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.UseShellExecute        = false;

                    process.Start();

                    string output = process.StandardOutput.ReadToEnd().Trim();
                    string error  = process.StandardError.ReadToEnd().Trim();

                    process.WaitForExit();
                    process.Close();

                    if (!string.IsNullOrEmpty(error))
                    {
                        EnqueueError(new ErrorEvent(ErrorCode.PARSE_FAILED, error));
                        return;
                    }

                    Debug.LogFormat(LOG_FORMAT, "Success getting media resources!");
                    MediaInfo info = JsonUtility.FromJson <MediaInfo>(output);

                    lock (parseCompletedQueue) parseCompletedQueue.Enqueue(info);
                }
                catch (Exception e)
                {
                    EnqueueError(new ErrorEvent(ErrorCode.PARSE_EXCEPTION, e.Message));
                    return;
                }

                // done
                status = ProcessingStatus.READY;

                #endregion
            }
        }
Пример #2
0
 protected void ExtractSupportedMediaFormats(MediaInfo mediaInfo)
 {
     supportedVideoFormats.Clear();
     supportedAudioFormats.Clear();
     foreach (MediaFormat format in mediaInfo.formats)
     {
         // Unity Video Player
         // TODO, currently only support h.264 with mp4, can better support by target platform
         // https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
         if (format.vcodec != "none" &&
             format.ext == "mp4" &&
             (format.protocol == "https" || format.protocol == "http"))
         {
             bool inserted = false;
             if (format.acodec != "none")
             {
                 // video/audio together
                 for (int i = 0; i < supportedVideoFormats.Count; i++)
                 {
                     // insert format based on video size
                     if (format.height == supportedVideoFormats[i].height)
                     {
                         // replace
                         supportedVideoFormats[i] = format;
                         inserted = true;
                         break;
                     }
                     else if (format.height < supportedVideoFormats[i].height)
                     {
                         // insert
                         supportedVideoFormats.Insert(i, format);
                         inserted = true;
                         break;
                     }
                 }
             }
             else
             {
                 // if (Application.platform == RuntimePlatform.Android)
                 // {
                 //   continue;
                 // }
                 // video only
                 for (int i = 0; i < supportedVideoFormats.Count; i++)
                 {
                     // check duplicated
                     if (format.height == supportedVideoFormats[i].height)
                     {
                         if (format.vcodec != null && format.vcodec.StartsWith("avc"))
                         {
                             // replace
                             supportedVideoFormats[i] = format;
                         }
                         inserted = true;
                         break;
                     }
                     else if (format.height < supportedVideoFormats[i].height)
                     {
                         // insert
                         supportedVideoFormats.Insert(i, format);
                         inserted = true;
                         break;
                     }
                 }
             }
             if (!inserted)
             {
                 // add to last
                 supportedVideoFormats.Add(format);
             }
         }
         else if (format.acodec != "none" &&
                  (format.ext == "mp3" || format.ext == "wav") && // mp3 only supported in android
                  (format.protocol == "https" || format.protocol == "http"))
         {
             // audio only
             supportedAudioFormats.Add(format);
         }
     }
 }