AddVideo() public method

public AddVideo ( Media video ) : void
video DataAccess.Media
return void
        protected override void OnCompleted(object sender, DownloadCompletedEventArgs e)
        {
            DownloadItemData IData          = e.DownloadInfo.Data as DownloadItemData;
            Media            Video          = IData.Media;
            string           Destination    = e.DownloadInfo.Destination;
            string           DestinationExt = Path.GetExtension(Destination);

            Destination = Destination.Substring(0, Destination.Length - Path.GetExtension(Destination).Length);

            // Ensure download and merge succeeded.
            if (!FileHasContent(e.DownloadInfo.Destination))
            {
                e.DownloadInfo.Status = DownloadStatus.Failed;
                return;
            }

            // Get final file name.
            DefaultMediaPath PathCalc    = new DefaultMediaPath();
            string           NewFileName = PathCalc.GetDefaultFileName(Video.Artist, Video.Title, Video.MediaCategoryId, (MediaType)Video.MediaTypeId);

            Directory.CreateDirectory(Path.GetDirectoryName(Settings.NaturalGroundingFolder + NewFileName));
            Video.FileName = NewFileName + DestinationExt;

            // Move file and overwrite.
            string DstFile = Settings.NaturalGroundingFolder + Video.FileName;

            if (File.Exists(DstFile))
            {
                FileOperationAPIWrapper.MoveToRecycleBin(DstFile);
            }
            File.Move(Destination + DestinationExt, DstFile);

            // Add to database
            EditVideoBusiness Business     = new EditVideoBusiness();
            Media             ExistingData = Business.GetVideoById(Video.MediaId);

            if (ExistingData != null)
            {
                // Edit video info.
                ExistingData.FileName = Video.FileName;
                ExistingData.Length   = null;
                ExistingData.Height   = null;
                Business.Save();
            }
            else
            {
                // Add new video info.
                Business.AddVideo(Video);
                Business.Save();
            }

            base.OnCompleted(sender, e);
        }
        private async Task DownloadCompletedAsync(DownloadItem downloadInfo) {
            // Separate file extension.
            string Destination = downloadInfo.Files[0].Destination;
            string DestinationExt = Path.GetExtension(Destination);
            Destination = Destination.Substring(0, Destination.Length - Path.GetExtension(Destination).Length);
            Media video = downloadInfo.Request;

            if (downloadInfo.Files.Count > 1) {
                VideoType File1Type = downloadInfo.Files[0].Source.VideoType;
                AudioType File2Type = downloadInfo.Files[1].Source.AudioType;
                string File1Ext = GetCodecExtension(File1Type);
                string File2Ext = GetAudioExtension(File2Type);
                DestinationExt = GetFinalExtension(File1Type, File2Type);

                // Make sure output file doesn't already exist.
                File.Delete(Destination + DestinationExt);

                // Merge audio and video files.
                await Task.Run(() => FfmpegBusiness.JoinAudioVideo(Destination + File1Ext, Destination + File2Ext, Destination + DestinationExt, true));

                // Delete source files
                File.Delete(Destination + File1Ext);
                File.Delete(Destination + File2Ext);
            } else if (downloadInfo.UpgradeAudio) {
                // Get original video format.
                MediaInfoReader MediaReader = new MediaInfoReader();
                //await MediaReader.LoadInfoAsync(Settings.NaturalGroundingFolder + downloadInfo.Request.FileName);
                string VideoDestExt = ".mkv";
                //if (MediaReader.VideoFormat == "VP8" || MediaReader.VideoFormat == "VP9")
                //    VideoDestExt = ".webm";
                string VideoDest = downloadInfo.Destination + " (extract)" + VideoDestExt;

                // Keep existing video and upgrade audio.
                string AudioExt = GetAudioExtension(downloadInfo.Files[0].Source.AudioType);
                DestinationExt = GetFinalExtension(Path.GetExtension(downloadInfo.Request.FileName), AudioExt);

                // Merge audio and video files.
                await Task.Run(() => {
                    FfmpegBusiness.ExtractVideo(Settings.NaturalGroundingFolder + downloadInfo.Request.FileName, VideoDest, true);
                    if (FileHasContent(VideoDest))
                        FfmpegBusiness.JoinAudioVideo(VideoDest, Destination + AudioExt, Destination + DestinationExt, true);
                });

                // Delete source files
                File.Delete(VideoDest);
                File.Delete(Destination + AudioExt);

                if (FileHasContent(Destination + DestinationExt) && File.Exists(Settings.NaturalGroundingFolder + downloadInfo.Request.FileName))
                    FileOperationAPIWrapper.MoveToRecycleBin(Settings.NaturalGroundingFolder + downloadInfo.Request.FileName);
            }

            // Ensure download and merge succeeded.
            if (FileHasContent(Destination + DestinationExt)) {
                // Get final file name.
                DefaultMediaPath PathCalc = new DefaultMediaPath();
                string NewFileName = PathCalc.GetDefaultFileName(video.Artist, video.Title, video.MediaCategoryId, (MediaType)video.MediaTypeId);
                Directory.CreateDirectory(Path.GetDirectoryName(Settings.NaturalGroundingFolder + NewFileName));
                video.FileName = NewFileName + DestinationExt;

                // Move file and overwrite.
                if (downloadInfo.Request.FileName != null && File.Exists(Settings.NaturalGroundingFolder + downloadInfo.Request.FileName))
                    FileOperationAPIWrapper.MoveToRecycleBin(Settings.NaturalGroundingFolder + downloadInfo.Request.FileName);
                File.Move(Destination + DestinationExt, Settings.NaturalGroundingFolder + video.FileName);
            } else
                throw new FileNotFoundException("Audio and video streams could not be merged.");

            // Add to database
            EditVideoBusiness Business = new EditVideoBusiness();
            Media ExistingData = Business.GetVideoById(video.MediaId);
            if (ExistingData != null) {
                // Edit video info.
                ExistingData.FileName = video.FileName;
                ExistingData.Length = null;
                ExistingData.Height = null;
                Business.Save();
            } else {
                // Add new video info.
                Business.AddVideo(video);
                Business.Save();
            }
            downloadInfo.Request.FileName = video.FileName;

            downloadInfo.Status = DownloadStatus.Done;
        }