SuRGeoNix / Flyleaf

Media Player .NET Library for WinUI 3/ WPF/WinForms (based on FFmpeg/DirectX)
GNU Lesser General Public License v3.0
711 stars 101 forks source link

How to write a wrapper class for the new version (3.3.0)? #77

Closed zydjohnHotmail closed 3 years ago

zydjohnHotmail commented 3 years ago

Hello: Today, I found there is a new version for Flyleaf, which is now at: 3.3.0. Unfortunately, my old C# code, I wrote for version 3.2.5 is not working any more. The following is my code: using FlyleafLib; using FlyleafLib.MediaFramework.MediaContext; using System.Diagnostics;

namespace FlyleafDownloader325 { public class FLDownloader { public static Downloader FlyleafHLSDownloader(int uniq_id, string url1, string video_file1) { Downloader Downloader1 = new(new Config.Demuxer() { MaxQueueSize = 1000 }, uniq_id); if (Downloader1.Open(url1) != 0) { Debug.Print("Could not open url input"); return null; } Downloader1.Demuxer.EnableStream(Downloader1.Demuxer.VideoStreams[0]); if (Downloader1.Demuxer.AudioStreams.Count != 0) Downloader1.Demuxer.EnableStream(Downloader1.Demuxer.AudioStreams[0]); Downloader1.Download(ref video_file1, true); return Downloader1; } } } Please let me know how I can change it for version 3.3.0? By the way, what is new in this version? Thanks,

SuRGeoNix commented 3 years ago

I wrote a new FlyleafDownloader Sample to fix any issues that I might come across and to demonstrate also the usage of the plugins inputs/streams etc.

I know that it is still too complex and I will try to simplify it in the future.

zydjohnHotmail commented 3 years ago

Hello: I found you have yet another new version of FlyLeaf, which is 3.3.1 now. It is good. But I still can’t figure out how to write one wrapper class. I like to have one wrapper class, since this way, I can totally ignore the download process after I found the M3U8 link, and my main program can continue to handle user interface, and put all the download job for background process. However, I read your sample code. I think the constructor for downloader is done via config, like this statement: Downloader = new Downloader(Config); But from your sample code, I can’t find how to provide any parameters for Config. For one downloader, I think I have to provide one unique ID, one integer number, and the M3U8 link (url), and finally one local file (MP4) to save the download. But from your sample code, at least for Config, there is nothing I can provide, or nothing I think is necessary for initializing a downloader. Please advise! Thanks,

SuRGeoNix commented 3 years ago

Did you clicked the link with the sample? I parse Config parameters in the constructor. You don't need to parse UniqueId anymore.

// Registers FFmpeg Libraries
Master.RegisterFFmpeg(":2");

// Prepares Player's Configuration
Config = new Config();
Config.Demuxer.FormatOpt.Add("probesize",(50 * (long)1024 * 1024).ToString());
Config.Demuxer.FormatOpt.Add("analyzeduration",(10 * (long)1000 * 1000).ToString());

Config.Demuxer.BufferDuration = 99999999999999;     // We might want to use small if we use both VideoDemuxer/AudioDemuxer to avoid having a lot of audio without video
Config.Demuxer.ReadTimeout = 60 * 1000 * 10000;     // 60 seconds to retry or fail
Config.Video.MaxVerticalResolutionCustom = 1080;    // Default Plugins Suggest based on this

// Initializes the Downloader
Downloader = new Downloader(Config);
Downloader.DownloadCompleted        += Downloader_DownloadCompleted;
zydjohnHotmail commented 3 years ago

Hello: There are things I don't quite understand now: Without UniqueID, I can understand. But I have to provide M3U8 url link for the downloader to fetch, and I also need to provide local file, like C:\download\1.mp4 to save the download results, right? If I don't provide these 2 parameters, how I can start the downloader to finish its job? I mainly use it to download M3U8 HLS stream and save in my local hard disk. Please advise!

SuRGeoNix commented 3 years ago

Read the sample

string error = Downloader.Open(txtUrl.Text);
Downloader.Download(ref filename, false); 
zydjohnHotmail commented 3 years ago

Hello: Thanks for your reply. I came up with the following C# code, for one wrapper class.

`using FlyleafLib; using FlyleafLib.MediaFramework.MediaContext; using FlyleafLib.MediaPlayer; using System.Diagnostics;

namespace FlyDownloader331 { public class FLDownloader { public static Downloader FlyleafHLSDownloader(string m3u8_link1, string local_mp4_file1) { Master.RegisterFFmpeg(":2"); Config Config1 = new(); Config1.Demuxer.FormatOpt.Add("probesize", (50 (long)1024 1024).ToString()); Config1.Demuxer.FormatOpt.Add("analyzeduration", (10 (long)1000 1000).ToString()); Config1.Demuxer.BufferDuration = 99999999999999;
Config1.Demuxer.ReadTimeout = 60 1000 10000;
Config1.Video.MaxVerticalResolutionCustom = 1080;
Downloader Downloader1 = new Downloader(Config1); if (!Downloader1.Open(m3u8_link1)) Downloader1.Download(ref local_mp4_file1, false); return Downloader1; } } } `

Now, I have one final issue: how I can detect that the download can’t open certain M3U8 link? I mean, how I can know if the open M3U8 link fails, then I should return null to the caller, so the caller knows that the underlying M3U8 link is not available? Thanks,

SuRGeoNix commented 3 years ago

Downloader.DownloadCompleted event will inform you

zydjohnHotmail commented 3 years ago

Thanks!

SuRGeoNix commented 3 years ago

Don't forget, the new downloader supports plugins which means you can download almost any web url video (that you can play from browser, even not just youtube but also facebook/instagram etc) using youtube-dl (might have some issues with some urls but generally works fine). Test the sample if you didn't which is included in the latest release.

zydjohnHotmail commented 3 years ago

Hello: I have now further question about the new version of Flyleaf library: I wrote the following C# code to call the wrapper class I made:

string m3u8_link1 = @"https://video1.maxline.by:444/o1/stream11/playlist.m3u8"; string mp4_file1 = @"C:\Videos\1.mp4" Downloader load_hls1 = FlyDownloader331.FLDownloader.FlyleafHLSDownloader(m3u8_link1, mp4_file1); load_hls1.Open(m3u8_link1); load_hls1.Download(ref mp4_file1);

For previous version of Flyleaf library, I can see immediately the MP4 file in the directory, like 1.mp4 in C:\Videos. However, when I used version 3.3.1 Flyleaf with my library, I can't see the MP4 files in the directory. I have no idea if the download is going or not. Let me know it is normal, I mean I should be able to see the local MP4 files after the download is finished. Or, it is not normal, I should see the MP4 file, even its size is changing all the time during download. Please advise!

SuRGeoNix commented 3 years ago

1) Don't use extension (.mp4). The downloader will choose for you, that way it ensures that it will find a compatible output container for the codecs.

2) When it downloads you should be able to see the file and the size increasing. However, you should register to property change event to get the CurTime/DownloadPercentage etc. (check the sample!)

zydjohnHotmail commented 3 years ago

Thanks, It looks OK now!