Madwin74

GetVideoURL

May 7th, 2023 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.77 KB | Source Code | 0 0
  1. /***********************************************************
  2.  **                                                       **
  3.  **   Codesnippets written by Madwin for MMP 2023         **
  4.  **                                                       **
  5.  **   Not to be used freely. the code still has errors    **
  6.  **   It's intended to be used as starting point.         **
  7.  **   Code has not been checked and might still need      **
  8.  **   (a lot of) work.                                    **
  9.  **                                                       **
  10.  **   © MadMedia Productions 2023                         **
  11.  **                                                       **
  12.  ***********************************************************/
  13.  
  14.  
  15. /*
  16. C# class that you can use to get the URL of a video stream
  17. playing in a web browser:
  18. */
  19.  
  20.  
  21. using System;
  22. using System.Windows.Forms;
  23.  
  24. namespace VideoStreamGetter
  25. {
  26.     public class VideoStreamGetter
  27.     {
  28.         private WebBrowser webBrowser;
  29.  
  30.         public VideoStreamGetter(WebBrowser webBrowser)
  31.         {
  32.             this.webBrowser = webBrowser;
  33.             this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
  34.         }
  35.  
  36.         private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  37.         {
  38.             foreach (HtmlElement videoElement in webBrowser.Document.GetElementsByTagName("video"))
  39.             {
  40.                 string src = videoElement.GetAttribute("src");
  41.                 Console.WriteLine("Video URL: " + src);
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. /*
  49. This class takes a WebBrowser object as a parameter in its
  50. constructor. It then attaches an event handler to the
  51. DocumentCompleted event of the web browser, which will be called
  52. when the web page finishes loading.
  53.  
  54. In the DocumentCompleted event handler, the class looks for
  55. all video elements in the web page using the GetElementsByTagName
  56. method of the HtmlDocument class. For each video element found,
  57. it retrieves the value of the src attribute using the GetAttribute
  58. method of the HtmlElement class, and prints it to the console.
  59.  
  60. To use this class, you can create an instance of it and pass
  61. in a WebBrowser object that you have already navigated to the
  62. web page containing the video stream. For example:
  63. */
  64.  
  65.  
  66. WebBrowser webBrowser = new WebBrowser();
  67. webBrowser.Navigate("https://example.com/video");
  68. VideoStreamGetter videoStreamGetter = new VideoStreamGetter(webBrowser);
  69.  
  70. /*
  71. Note that this class assumes that there is only one video element
  72. in the web page. If there are multiple video elements, you will
  73. need to modify the code to handle them appropriately.
  74.  
  75.  
  76. Updated version of the C# class that retrieves the URL of a
  77. video stream playing in a WebBrowser control, this time by
  78. getting it from a metafile instead of directly from the video element:
  79. */
  80.  
  81.  
  82. using System;
  83. using System.Windows.Forms;
  84.  
  85. namespace VideoStreamGetter
  86. {
  87.     public class VideoStreamGetter
  88.     {
  89.         private WebBrowser webBrowser;
  90.  
  91.         public VideoStreamGetter(WebBrowser webBrowser)
  92.         {
  93.             this.webBrowser = webBrowser;
  94.             this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
  95.         }
  96.  
  97.         private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  98.         {
  99.             foreach (HtmlElement metaElement in webBrowser.Document.GetElementsByTagName("meta"))
  100.             {
  101.                 if (metaElement.GetAttribute("name") == "twitter:player:stream")
  102.                 {
  103.                     string src = metaElement.GetAttribute("content");
  104.                     Console.WriteLine("Video URL: " + src);
  105.                 }
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. /*
  112. This class works similarly to the previous version, but instead of
  113. looking for video elements, it looks for meta elements with the name
  114. attribute set to "twitter:player:stream". This is a common way of
  115. specifying a video stream URL in a web page.
  116.  
  117. For each meta element found, the class retrieves the value of the
  118. content attribute using the GetAttribute method of the HtmlElement
  119. class, and prints it to the console.
  120.  
  121. To use this class, you can create an instance of it and pass in a
  122. WebBrowser object that you have already navigated to the web page
  123. containing the video stream. For example:
  124. */
  125.  
  126.  
  127. WebBrowser webBrowser = new WebBrowser();
  128. webBrowser.Navigate("https://example.com/video");
  129. VideoStreamGetter videoStreamGetter = new VideoStreamGetter(webBrowser);
  130.  
  131. /*
  132. Note that this class assumes that there is only one meta element with the
  133. name attribute set to "twitter:player:stream". If there are multiple such
  134. elements, you will need to modify the code to handle them appropriately.
  135. */
Advertisement
Add Comment
Please, Sign In to add comment