Madwin74

VideostreamRecorder

May 7th, 2023 (edited)
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 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 uses the VLC library to record a video stream:
  17.  
  18.  
  19.  
  20. using System;
  21. using System.IO;
  22. using Vlc.DotNet.Core;
  23. using Vlc.DotNet.Core.Interops;
  24. using Vlc.DotNet.Core.Interops.Signatures.LibVlc.MediaPlayer;
  25.  
  26. namespace VideoRecorder
  27. {
  28.     public class VlcVideoRecorder
  29.     {
  30.         private VlcMediaPlayer _player;
  31.         private VlcMedia _media;
  32.  
  33.         public void StartRecording(string streamUrl, string outputFilePath)
  34.         {
  35.             var vlcOptions = new string[]
  36.             {
  37.                 "--no-audio",      // Disable audio recording
  38.                 "--sout=#transcode{vcodec=h264,vb=2000,fps=30}:file{dst=" + outputFilePath + "}" // Transcode and save to file
  39.             };
  40.  
  41.             // Initialize VLC library
  42.             VlcContext.LibVlcAddOption(vlcOptions);
  43.             VlcContext.Initialize();
  44.  
  45.             // Create media player and media objects
  46.             _player = new VlcMediaPlayer(VlcContext);
  47.             _media = new VlcMedia(VlcContext, streamUrl, FromType.FromLocation);
  48.  
  49.             // Set media options
  50.             _media.AddOption(":network-caching=1000");  // Increase network caching to prevent stuttering
  51.  
  52.             // Attach media to player and start playing
  53.             _player.SetMedia(_media);
  54.             _player.Play();
  55.         }
  56.  
  57.         public void StopRecording()
  58.         {
  59.             // Stop recording and release resources
  60.             _player.Stop();
  61.             _media.Dispose();
  62.             _player.Dispose();
  63.             VlcContext.CloseAll();
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. /*
  70. To use this class, you can call the StartRecording method with
  71. the URL of the video stream you want to record and the file path
  72. where you want to save the recorded video. For example:
  73. */
  74.  
  75.  
  76. var recorder = new VlcVideoRecorder();
  77. recorder.StartRecording("http://example.com/stream", "output.mp4");
  78.  
  79. // Wait for some time while recording...
  80. System.Threading.Thread.Sleep(5000);
  81.  
  82. recorder.StopRecording();
  83.  
  84.  
  85. /*
  86. This will start recording the video stream and save it to a file
  87. called "output.mp4". You can adjust the recording options by modifying
  88. the vlcOptions array in the StartRecording method. For example, you can
  89. change the video codec, bitrate, or frame rate by modifying the vcodec,
  90. vb, or fps options, respectively.
  91. */
Advertisement
Add Comment
Please, Sign In to add comment