Exemplo n.º 1
0
    private string convWMV(string _mediaName)
    {
        string ret = "";
        // Sample code for encoding any format video to wmv format.
        MediaHandler _mhandler = new MediaHandler();

        // required properties

        string rootpath = Server.MapPath(Request.ApplicationPath);
        string inputpath = Server.MapPath("~/lib/up/v/");
        string outputpath = Server.MapPath("~/lib/up/v/");
        string _ffmpegpath = HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.dll");

        string filenameWMV = Guid.NewGuid().ToString().Substring(0, 10) + ".wmv";
        _mhandler.FFMPEGPath = _ffmpegpath;
        _mhandler.InputPath = inputpath;
        _mhandler.OutputPath = outputpath;
        _mhandler.FileName = _mediaName;

        // optional output filename

        _mhandler.OutputFileName = filenameWMV;

        // setting video related properties

        //_mhandler.Video_Bitrate = 786;
        //_mhandler.Audio_Bitrate = 64;
        //_mhandler.Audio_SamplingRate = 44100;

        // generate highest quality mp4 video, note by making this option true, video bitrate will no longer work.

        _mhandler.MaxQuality = true;

        // optional video width and height settings

        _mhandler.Width = 538;
        _mhandler.Height = 400;

        // Optional parameters for setting audio and video codec for wmv video
        _mhandler.VCodec = "wmv2";
        _mhandler.ACodec = "wmav2";
        _mhandler.Audio_Bitrate = 64;
        _mhandler.Video_Bitrate = 1000;

        // posting watermark on wmv video, view detail in watermark section
        //  _mhandler.WaterMarkPath = RootPath + "\\contents\\watermark";
        //  _mhandler.WaterMarkImage = "watermark.gif";

        // view more options in component documentation.

        // Encode WMV Video using Media Handler Pro version 3.0

        //  string output = _mhandler.Encode_WMV();

        // Encode WMV Video using Media Handler Pro version 4.0 or later

        VideoInfo info = _mhandler.Encode_WMV();

        // Check for errors
        if (info.ErrorCode > 0)
        {
            Response.Write("Video processing failed, Error code " + info.ErrorCode + " generated");
            return "";
        }
        ret = filenameWMV;
        return filenameWMV;
    }
Exemplo n.º 2
0
        private int EncodeVideo(DataObjectVideo video, string format, VideoVersion videoVersion, Dictionary <string, string> defaultSettings, Dictionary <string, string> actionSettings, bool deleteEncodedVideo)
        {
            //Console.WriteLine("Encoding '" + video.Title + "' -> " + video.OriginalLocation);

            FileInfo  sourceVideoFileInfo = new FileInfo(video.OriginalLocation);
            VideoInfo encodedVideoInfo    = new VideoInfo();

            MediaHandler mediaHandler = new MediaHandler();

            mediaHandler.FFMPEGPath     = ffmpegExecutable;
            mediaHandler.InputPath      = sourceVideoFileInfo.DirectoryName;
            mediaHandler.OutputPath     = temporaryConversionFolder;
            mediaHandler.FileName       = sourceVideoFileInfo.Name;
            mediaHandler.OutputFileName = string.Format("{2}{0}.{1}", video.ObjectID, format, videoVersion);

            VideoInfo sourceVideoInfo = mediaHandler.Get_Info();

            if (!video.OriginalLocation.ToLower().EndsWith(string.Format(".{0}", format)))
            {
                try
                {
                    if (actionSettings.ContainsKey("VideoWidth"))
                    {
                        //An empty VideoWidth overides the default setting width
                        if (!string.IsNullOrEmpty(actionSettings["VideoWidth"]))
                        {
                            mediaHandler.Width = Convert.ToInt32(actionSettings["VideoWidth"]);
                            if (actionSettings.ContainsKey("VideoHeight") && !string.IsNullOrEmpty(actionSettings["VideoHeight"]) && actionSettings["VideoHeight"] != "auto")
                            {
                                mediaHandler.Height = Convert.ToInt32(actionSettings["VideoHeight"]);
                            }
                            else
                            {
                                int calculatedHeight = (int)(mediaHandler.Width * (double)sourceVideoInfo.Height / (double)sourceVideoInfo.Width);
                                if (calculatedHeight % 2 != 0)
                                {
                                    calculatedHeight += 1;
                                }
                                mediaHandler.Height = calculatedHeight;
                            }
                        }
                    }
                    else if (defaultSettings.ContainsKey("VideoWidth") && !string.IsNullOrEmpty(defaultSettings["VideoWidth"]))
                    {
                        mediaHandler.Width = Convert.ToInt32(defaultSettings["VideoWidth"]);
                        if (defaultSettings.ContainsKey("VideoHeight") && !string.IsNullOrEmpty(defaultSettings["VideoHeight"]) && defaultSettings["VideoHeight"] != "auto")
                        {
                            mediaHandler.Height = Convert.ToInt32(defaultSettings["VideoHeight"]);
                        }
                        else
                        {
                            int calculatedHeight = (int)(mediaHandler.Width * (double)sourceVideoInfo.Height / (double)sourceVideoInfo.Width);
                            if (calculatedHeight % 2 != 0)
                            {
                                calculatedHeight += 1;
                            }
                            mediaHandler.Height = calculatedHeight;
                        }
                    }
                }
                catch
                {
                }
                try
                {
                    if (actionSettings.ContainsKey("VideoFrameRate"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["VideoFrameRate"]) && actionSettings["VideoFrameRate"] != "auto")
                        {
                            mediaHandler.FrameRate = Convert.ToDouble(actionSettings["VideoFrameRate"]);
                        }
                    }
                    else if (defaultSettings.ContainsKey("VideoFrameRate") && !string.IsNullOrEmpty(defaultSettings["VideoFrameRate"]) && defaultSettings["VideoFrameRate"] != "auto")
                    {
                        mediaHandler.FrameRate = Convert.ToInt32(defaultSettings["VideoFrameRate"]);
                    }
                }
                catch
                {
                }
                try
                {
                    if (actionSettings.ContainsKey("StartPosition"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["StartPosition"]))
                        {
                            mediaHandler.Start_Position = actionSettings["StartPosition"];
                        }
                    }
                    else if (defaultSettings.ContainsKey("StartPosition") && !string.IsNullOrEmpty(defaultSettings["StartPosition"]))
                    {
                        mediaHandler.Start_Position = defaultSettings["StartPosition"];
                    }
                }
                catch
                {
                }
                try
                {
                    if (actionSettings.ContainsKey("Duration"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["Duration"]))
                        {
                            mediaHandler.Duration = actionSettings["Duration"];
                        }
                    }
                    else if (defaultSettings.ContainsKey("Duration") && !string.IsNullOrEmpty(defaultSettings["Duration"]))
                    {
                        mediaHandler.Duration = defaultSettings["Duration"];
                    }
                }
                catch
                {
                }
                try
                {
                    double sourceVideoBitrate;
                    double.TryParse(sourceVideoInfo.Video_Bitrate.Replace(" kb/s", ""), out sourceVideoBitrate);
                    mediaHandler.Video_Bitrate = sourceVideoBitrate;
                    if (actionSettings.ContainsKey("VideoBitrate"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["VideoBitrate"]) && actionSettings["VideoBitrate"] != "auto")
                        {
                            if (sourceVideoBitrate > Convert.ToDouble(actionSettings["VideoBitrate"]) || sourceVideoBitrate == 0)
                            {
                                mediaHandler.Video_Bitrate = Convert.ToDouble(actionSettings["VideoBitrate"]);
                            }
                        }
                    }
                    else if (defaultSettings.ContainsKey("VideoBitrate") && !string.IsNullOrEmpty(defaultSettings["VideoBitrate"]) && defaultSettings["VideoBitrate"] != "auto")
                    {
                        if (sourceVideoBitrate > Convert.ToDouble(defaultSettings["VideoBitrate"]) || sourceVideoBitrate == 0)
                        {
                            mediaHandler.Video_Bitrate = Convert.ToInt32(defaultSettings["VideoBitrate"]);
                        }
                    }
                }
                catch
                {
                }
                try
                {
                    double sourceAudioBitrate;
                    double.TryParse(sourceVideoInfo.Audio_Bitrate.Replace(" kb/s", ""), out sourceAudioBitrate);
                    mediaHandler.Audio_Bitrate = sourceAudioBitrate;
                    if (actionSettings.ContainsKey("AudioBitrate"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["AudioBitrate"]) && actionSettings["AudioBitrate"] != "auto")
                        {
                            if (sourceAudioBitrate > Convert.ToDouble(actionSettings["AudioBitrate"]) || sourceAudioBitrate == 0)
                            {
                                mediaHandler.Audio_Bitrate = Convert.ToDouble(actionSettings["AudioBitrate"]);
                            }
                        }
                    }
                    else if (defaultSettings.ContainsKey("AudioBitrate") && !string.IsNullOrEmpty(defaultSettings["AudioBitrate"]) && defaultSettings["AudioBitrate"] != "auto")
                    {
                        if (sourceAudioBitrate > Convert.ToDouble(defaultSettings["VideoBitrate"]) || sourceAudioBitrate == 0)
                        {
                            mediaHandler.Audio_Bitrate = Convert.ToInt32(defaultSettings["AudioBitrate"]);
                        }
                    }
                }
                catch
                {
                }
                try
                {
                    int sourceAudioSamplingRate;
                    int.TryParse(sourceVideoInfo.SamplingRate.Replace(" kb/s", ""), out sourceAudioSamplingRate);
                    mediaHandler.Audio_SamplingRate = sourceAudioSamplingRate == 32000 ? 44100 : sourceAudioSamplingRate;
                    if (actionSettings.ContainsKey("AudioSamplingRate"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["AudioSamplingRate"]) && actionSettings["AudioSamplingRate"] != "auto")
                        {
                            if (sourceAudioSamplingRate > Convert.ToDouble(actionSettings["AudioSamplingRate"]) || sourceAudioSamplingRate == 0)
                            {
                                mediaHandler.Audio_SamplingRate = Convert.ToInt32(actionSettings["AudioSamplingRate"]);
                            }
                        }
                    }
                    else if (defaultSettings.ContainsKey("AudioSamplingRate") && !string.IsNullOrEmpty(defaultSettings["AudioSamplingRate"]) && defaultSettings["AudioSamplingRate"] != "auto")
                    {
                        if (sourceAudioSamplingRate > Convert.ToDouble(defaultSettings["VideoBitrate"]) || sourceAudioSamplingRate == 0)
                        {
                            mediaHandler.Audio_SamplingRate = Convert.ToInt32(defaultSettings["AudioSamplingRate"]);
                        }
                    }
                }
                catch
                {
                }

                // Force mono or stereo, no 5:1 supported
                mediaHandler.Channel = sourceVideoInfo.Channel.Contains("mono") ? 1 : 2;

                mediaHandler.ExitProcess = 100000;
                try
                {
                    if (actionSettings.ContainsKey("MaxDecodingTime"))
                    {
                        if (!string.IsNullOrEmpty(actionSettings["MaxDecodingTime"]))
                        {
                            mediaHandler.ExitProcess = Convert.ToInt32(actionSettings["MaxDecodingTime"]);
                        }
                    }
                    else if (defaultSettings.ContainsKey("MaxDecodingTime"))
                    {
                        mediaHandler.ExitProcess = Convert.ToInt32(defaultSettings["MaxDecodingTime"]);
                    }
                }
                catch
                {
                }
                try
                {
                    bool embeddedWatermark = false;
                    if (actionSettings.ContainsKey("EmbeddedWatermark"))
                    {
                        embeddedWatermark = bool.Parse(actionSettings["EmbeddedWatermark"]);
                    }
                    else if (defaultSettings.ContainsKey("EmbeddedWatermark"))
                    {
                        embeddedWatermark = bool.Parse(defaultSettings["EmbeddedWatermark"]);
                    }

                    if (embeddedWatermark)
                    {
                        string watermarkImage = string.Empty;
                        if (actionSettings.ContainsKey("WatermarkImage"))
                        {
                            watermarkImage = actionSettings["WatermarkImage"];
                        }
                        else if (defaultSettings.ContainsKey("WatermarkImage"))
                        {
                            watermarkImage = defaultSettings["WatermarkImage"];
                        }

                        watermarkFileInfo = CreateWatermarkFrame(video, sourceVideoInfo, mediaHandler, watermarkImage);
                        if (watermarkFileInfo != null)
                        {
                            mediaHandler.WaterMarkPath  = watermarkFileInfo.DirectoryName;
                            mediaHandler.WaterMarkImage = watermarkFileInfo.Name;
                        }
                    }
                }
                catch
                {
                }
                switch (format)
                {
                case "wmv":
                    encodedVideoInfo = mediaHandler.Encode_WMV();
                    break;

                case "mpg":
                    encodedVideoInfo = mediaHandler.Encode_MPG();
                    break;

                case "mp4":
                    encodedVideoInfo = mediaHandler.Encode_MP4();
                    break;

                case "mov":
                    encodedVideoInfo = mediaHandler.Encode_MOV();
                    break;

                case "m4v":
                    encodedVideoInfo = mediaHandler.Encode_M4V();
                    break;

                case "flv":
                    encodedVideoInfo = mediaHandler.Encode_FLV();
                    break;

                case "avi":
                    encodedVideoInfo = mediaHandler.Encode_AVI();
                    break;

                case "3gp":
                    encodedVideoInfo = mediaHandler.Encode_3GP();
                    break;
                }

                if (encodedVideoInfo.ErrorCode == 0)
                {
                    FileInfo encodedVideoFileInfo = new FileInfo(string.Format(@"{0}\{3}{1}.{2}", temporaryConversionFolder, video.ObjectID, format, videoVersion));
                    if (encodedVideoFileInfo.Length == 0)
                    {
                        throw new Exception("Video processing failed: Filesize 0 bytes");
                    }
                }
            }
            else // Already a video in the given format
            {
                File.Copy(video.OriginalLocation, string.Format(@"{0}\{3}{1}.{2}", temporaryConversionFolder, video.ObjectID, format, videoVersion));
            }
            //Move generated Video

            string encodedVideoFile = string.Format(@"{0}\{3}{1}.{2}", temporaryConversionFolder, video.ObjectID, format, videoVersion);

            if (useAmazoneS3)
            {
                UploadToAmazonS3(encodedVideoFile, video.ObjectID.Value, video.UserID.Value, format, videoVersion);
            }
            else
            {
                UploadToMediaFolder(encodedVideoFile, video.ObjectID.Value, video.UserID.Value, format, videoVersion);
            }
            if (deleteEncodedVideo)
            {
                DeleteFile(string.Format(@"{0}\{3}{1}.{2}", temporaryConversionFolder, video.ObjectID, format, videoVersion));
            }
            return(encodedVideoInfo.ErrorCode);
        }
Exemplo n.º 3
0
    private string convWMV(string _mediaName)
    {
        string ret = "";
        // Sample code for encoding any format video to wmv format.
        MediaHandler _mhandler = new MediaHandler();

        // required properties

        string rootpath    = Server.MapPath(Request.ApplicationPath);
        string inputpath   = Server.MapPath("~/lib/up/v/");
        string outputpath  = Server.MapPath("~/lib/up/v/");
        string _ffmpegpath = HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.dll");

        string filenameWMV = Guid.NewGuid().ToString().Substring(0, 10) + ".wmv";

        _mhandler.FFMPEGPath = _ffmpegpath;
        _mhandler.InputPath  = inputpath;
        _mhandler.OutputPath = outputpath;
        _mhandler.FileName   = _mediaName;

        // optional output filename

        _mhandler.OutputFileName = filenameWMV;

        // setting video related properties

        //_mhandler.Video_Bitrate = 786;
        //_mhandler.Audio_Bitrate = 64;
        //_mhandler.Audio_SamplingRate = 44100;

        // generate highest quality mp4 video, note by making this option true, video bitrate will no longer work.

        _mhandler.MaxQuality = true;

        // optional video width and height settings

        _mhandler.Width  = 538;
        _mhandler.Height = 400;

        // Optional parameters for setting audio and video codec for wmv video
        _mhandler.VCodec        = "wmv2";
        _mhandler.ACodec        = "wmav2";
        _mhandler.Audio_Bitrate = 64;
        _mhandler.Video_Bitrate = 1000;

        // posting watermark on wmv video, view detail in watermark section
        //  _mhandler.WaterMarkPath = RootPath + "\\contents\\watermark";
        //  _mhandler.WaterMarkImage = "watermark.gif";

        // view more options in component documentation.

        // Encode WMV Video using Media Handler Pro version 3.0

        //  string output = _mhandler.Encode_WMV();

        // Encode WMV Video using Media Handler Pro version 4.0 or later

        VideoInfo info = _mhandler.Encode_WMV();

        // Check for errors
        if (info.ErrorCode > 0)
        {
            Response.Write("Video processing failed, Error code " + info.ErrorCode + " generated");
            return("");
        }
        ret = filenameWMV;
        return(filenameWMV);
    }