Jump to content

[Help] I have a problem saveing a video after downloading [C#]

- - - - -

  • Please log in to reply
No replies to this topic

#1
lil-fino

lil-fino

    Newbie

  • Members
  • PipPip
  • 15 posts
I dont know what i am doing wrong i am trying to make a youtube downloader but after i download the video i try to save it as .flv file but it doesnt work.. tell me what im doing wrong

this is my code


using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Drawing;

using System.Diagnostics;

using System.Windows.Forms;

using System.Linq;

using System.Xml.Linq;

using System.Net;

using System.IO;


namespace YouTube_Getter

{

    public partial class Form1 : Form

    {

      

        public Form1()

        {

            InitializeComponent();

        }

        private byte[] downloadedData;

        private Video_Downloader_DLL.VideoDownloader downloadmaker = new Video_Downloader_DLL.VideoDownloader();

        private void Button1_Click(object sender, System.EventArgs e)

        {

            string videotitle = "";

            string output = "";

            string inputurl = TextBox1.Text;

            downloadmaker.GetVideoTitle(inputurl, ref videotitle);

            System.String tempVar = "";

            System.Int32 tempVar2 = 0;

            downloadmaker.MakeDownloadURL(inputurl, ref output, ref tempVar, ref tempVar2);

            OutLink.Text = (videotitle);

            LinkText.Text = (output);

            MessageBox.Show("To Download " + videotitle + Environment.NewLine + "Please click Download now, When finished Downloading select Save To File");

        }

        private void downloadData(string url)

        {

            progressBar1.Value = 0;


            downloadedData = new byte[0];

            try

            {

                this.Text = "Connecting...";

                Application.DoEvents();


                WebRequest req = WebRequest.Create(url);

                WebResponse response = req.GetResponse();

                Stream stream = response.GetResponseStream();


                byte[] buffer = new byte[1024];


                int dataLength = (int)response.ContentLength;


                progressBar1.Maximum = dataLength;

                lbProgress.Text = "0/" + dataLength.ToString();


                this.Text = "Downloading...";

                Application.DoEvents();


                MemoryStream memStream = new MemoryStream();

                while (true)

                {

                   

                    int bytesRead = stream.Read(buffer, 0, buffer.Length);


                    if (bytesRead == 0)

                    {

                      

                        progressBar1.Value = progressBar1.Maximum;

                        lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();


                        Application.DoEvents();

                        break;

                    }

                    else

                    {

                        

                        memStream.Write(buffer, 0, bytesRead);


                        

                        if (progressBar1.Value + bytesRead <= progressBar1.Maximum)

                        {

                            progressBar1.Value += bytesRead;

                            lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString();


                            progressBar1.Refresh();

                            Application.DoEvents();

                        }

                    }

                }


            

                downloadedData = memStream.ToArray();


             

                stream.Close();

                memStream.Close();

            }

            catch (Exception)

            {

               

                MessageBox.Show("There was an error accessing the URL.");

            }


            txtData.Text = downloadedData.Length.ToString();

            this.Text = "YT Wdownloader";

        }


        private void btnDownload_Click(object sender, EventArgs e)

        {

            downloadData(LinkText.Text);


            

            if (downloadedData != null && downloadedData.Length != 0)

            {

                string ytdata = OutLink.Text;

                string urlName = LinkText.Text;

                if (urlName.EndsWith("/"))

                    urlName = urlName.Substring(0, urlName.Length - 1); 


                urlName = urlName.Substring(urlName.LastIndexOf('/') + 1);


                saveDiag1.FileName = ytdata + ".flv";

            }


        }


        private void button2_Click(object sender, EventArgs e)

        {

            if (downloadedData != null && downloadedData.Length != 0)

            {

                if (saveDiag1.ShowDialog() == DialogResult.OK)

                {

                    this.Text = "Saving Data...";

                    Application.DoEvents();


                    

                    FileStream newFile = new FileStream(saveDiag1.FileName, FileMode.Create);

                    newFile.Write(downloadedData, 0, downloadedData.Length);

                    newFile.Close();


                    this.Text = "Download Data";

                    MessageBox.Show("Saved Successfully");

                }

            }

            else

                MessageBox.Show("No File was Downloaded Yet!");

        }


        private void Form1_Load(object sender, EventArgs e)

        {


        }

    }

}


        


    







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users