Click here to Skip to main content
15,891,943 members
Articles / Multimedia / GDI+

MyDownloader: A Multi-thread C# Segmented Download Manager

Rate me:
Please Sign up or sign in to vote.
4.95/5 (418 votes)
12 Feb 2008CPOL10 min read 2.2M   124.7K   1.3K  
Sample application that manages multiple segmented downloads and supports HTTP, FTP and YouTube video downloads
using System;
using System.Collections.Generic;
using System.Text;
using MyDownloader.Core;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace MyDownloader.Extension.Video
{
    [Serializable]
    public enum VideoFormat: int
    {
        None, 
        AVI,
        MPEG,
        MP3
    }

    public static class VideoConverter
    {
        private const string VideoConverterFormatProperty = "VideoConverterFormatProperty";

        private const string ffmpeg = "ffmpeg.exe";

        private const string ToAVICmdLine = "-y -i \"{0}\" -vcodec msmpeg4v2 -acodec mp3 -sameq -ab 128 \"{1}\"";
        private const string ToMPEGCmdLine = "-y -i \"{0}\" -vcodec mpeg1video -acodec mp3 -sameq -ab 64 \"{1}\"";
        private const string ToMP3CmdLine = "-y -i \"{0}\" -f mp3 -vn -acodec copy \"{1}\"";

        public static void Convert(string fileName, VideoFormat fmt)
        {
            if (fmt == VideoFormat.None) throw new ArgumentException("Invalid argmument.", "fmt");

            string cmd = String.Empty;

            if (fmt == VideoFormat.AVI) cmd = ToAVICmdLine;
            if (fmt == VideoFormat.MPEG) cmd = ToMPEGCmdLine;
            if (fmt == VideoFormat.MP3) cmd = ToMP3CmdLine;
            
            string directory = Path.GetDirectoryName(fileName);

            // the original file name ends with 'AVI', 'MPEG', 'MP3'....new we rename the file to .FLV
            
            string flvPath = Path.Combine(directory, String.Concat(Guid.NewGuid().ToString("N"), ".flv"));
            
            try
            {
                File.Move(fileName, flvPath);

                string sourceFile = Path.GetFileName(flvPath);
                string targetFile = Path.GetFileName(fileName);

                cmd = String.Format(cmd, sourceFile, targetFile);

                string toolPath = Path.GetDirectoryName(Application.ExecutablePath);
                //toolPath = @"C:\Documents and Settings\guilherme.labigalini\Desktop\MyDownloader\MyDownloader\MyDownloader.App\bin\Debug\";
                ProcessStartInfo psi = new ProcessStartInfo(toolPath + "\\" + ffmpeg, cmd);
                psi.UseShellExecute = false;
                psi.CreateNoWindow = true;
                psi.WorkingDirectory = directory;

                using (Process p = Process.Start(psi))
                {
                    p.WaitForExit();
                }
            }
            finally
            {
                File.Delete(flvPath);
            }            
        }

        public static void ConvertIfNecessary(Downloader d)
        {
            VideoFormat fmt = GetConvertOption(d);

            if (fmt == VideoFormat.None)
            {
                return;
            }

            try
            {
                d.StatusMessage = "converting video";

                Convert(d.LocalFile, fmt);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                d.LastError = new Exception("error converting video: " + ex.Message, ex);
            }
            finally
            {
                d.StatusMessage = null;
            }
        }

        public static VideoFormat GetConvertOption(Downloader d)
        {
            if (! d.ExtendedProperties.ContainsKey(VideoConverterFormatProperty))
            {
                return VideoFormat.None;
            }

            object objFmt = d.ExtendedProperties[VideoConverterFormatProperty];

            if (objFmt == null)
            {
                return VideoFormat.None;
            }

            return (VideoFormat)objFmt;
        }

        public static void SetConvertOption(Downloader d, VideoFormat format)
        {
            if (format == VideoFormat.None)
            {
                if (d.ExtendedProperties.ContainsKey(VideoConverterFormatProperty))
                {
                    d.ExtendedProperties.Remove(VideoConverterFormatProperty);
                }
            }

            d.ExtendedProperties[VideoConverterFormatProperty] = format;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions