Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 169
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Youtube Download Klasse für C#


Autor: Kevin Gerndt
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 3307
  
Kick it on dotnet-kicks.de  

Beschreibung:

Mit dieser Klasse lassen sich Videos im auf Youtube vorliegenden Format herunterladen.

Abgelegt unter: Youtube, C#, Youtubevideo.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;

class YoutubeDownload
{
    public YoutubeDownload()
    {
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
        webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
    }

    private WebClient webClient = new WebClient();

    public delegate void DownloadVideoCompletedEventhandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e);
    public event DownloadVideoCompletedEventhandler DownloadVideoCompleted;

    public delegate void DownloadVideoProgressChangedEventhandler(object sender, DownloadProgressChangedEventArgs e);
    public event DownloadVideoProgressChangedEventhandler DownloadVideoProgressChanged;

    public string GetYoutubeUrl(string youtubeUrl)
    {
        string flashvars = "";
        string fmt_stream_map = "";
        string[] seperator = { "%7C" };

        Regex flashvarsRegex = new Regex("(?<flashvars>flashvars=\".*\")");
        Regex fmt_stream_mapRegex = new Regex("(?<fmt_stream_map>fmt_stream_map=.*\")");

        flashvars = flashvarsRegex.Match(webClient.DownloadString(youtubeUrl)).Groups["flashvars"].Value;

        fmt_stream_map = fmt_stream_mapRegex.Match(flashvars).Groups["fmt_stream_map"].Value;

        return HttpUtility.UrlDecode(fmt_stream_map.Split(seperator, StringSplitOptions.RemoveEmptyEntries)[5]);
    }

    public void DownloadVideo(string downloadUrl, string fullFilename)
    {
        webClient.DownloadFileAsync(new Uri(downloadUrl), fullFilename);
    }

    private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        if (DownloadVideoProgressChanged != null)
        {
            DownloadVideoProgressChanged(sender, e);
        }
    }

    private void webClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (DownloadVideoCompleted != null)
        {
            DownloadVideoCompleted(sender, e);
        }
    }

    public string GetVideoTitle(string youtubeUrl)
    {
        Regex titleRegex = new Regex("title=\".*\"");

        string title = titleRegex.Match(webClient.DownloadString(youtubeUrl)).NextMatch().Value;

        title = title.Replace("title=\"", "");

        title = title.Remove((title.Length - 1), 1);

        return title;
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.