0

I'm trying to save a live video stream to storage in Android App using libvlc. I can do it on PC with command line and it work fine, I record the file and can view it afterwards.

But in the app the file records, it's only 151B big which is probably empty and if I try to open it I get message "Cannot play this video format"

My question is, is it possible to record to storage in Android with libvlc?

I'm fairly new to programming so any suggestions would help

    const string VIDEO_URL = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";

    public MainPage()
    {
        InitializeComponent();
        Core.Initialize();           
        using (var libvlc = new LibVLC())
        using (var mediaPlayer = new MediaPlayer(libvlc))
        {
            var media = new Media(libvlc, VIDEO_URL, FromType.FromLocation);
            var currentDirectory = "/storage/emulated/0/dcim/";
            var destination = Path.Combine(currentDirectory, "record4.mp4");

            // Define stream output options. 
            // In this case stream to a file with the given path and play locally the stream while streaming it.
            media.AddOption(":sout=#transcode{vcodec=h264}:std{access=file,dst=" + destination + "}");

            // Start recording
             mediaPlayer.Play(media);
        }
    }   

2 Answers2

0

The mediaplayer must be stop be for closing the app. Here is a interim solution. Will update it as soon as I have a proper solution.

   public partial class MainPage : ContentPage
{
    const string VIDEO_URL = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";

    public MainPage()
    {
        InitializeComponent();
        Core.Initialize();           
        using (var libvlc = new LibVLC())
        using (var mediaPlayer = new MediaPlayer(libvlc))
        {
            var media = new Media(libvlc, VIDEO_URL, FromType.FromLocation);
            var currentDirectory = "/storage/emulated/0/dcim/";
            var destination = Path.Combine(currentDirectory, "record7.mp4");

            // Define stream output options. 
            // In this case stream to a file with the given path and play locally the stream while streaming it.

            media.AddOption(":sout=#file{dst=" + destination + "}");
            media.AddOption(":sout-keep");


            // Start recording
             mediaPlayer.Play(media);

            *// Added these lines that create event handler error for Xamarin.forms, 
            // but at least it stops mediaplayer before closing App. and the 
            // recording can be play afterwards.*
            Console.WriteLine($"Recording in {destination}");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}
0

First create a destination file and also parse URL as URI not as a path, add following arg to libvlc

Final ArrayList<String> args = new ArrayList<>();
args.add (" &ndash; aout=opensles");
args.add (" &ndash; vout=android_display");
args.add (" &ndash; audio-time-stretch");
args.add (" &ndash; no-sub-autodetect-file");
args.add (" &ndash; swscale-mode=0");
args.add (" &ndash; network-caching=500");
args.add (" &ndash; no-drop-late-frames");
args.add (" &ndash; no-skip-frames");
args.add (" &ndash; avcodec-skip-frame");
args.add (" &ndash; avcodec-hw=any");
mLibVLC = new LibVLC (mContext, args);
David Buck
  • 3,752
  • 35
  • 31
  • 35
Akshay Modi
  • 1
  • 1
  • 1