public bool UpdateFileName(VideoListItem item, string fileName) { EditVideoBusiness Business = new EditVideoBusiness(); if (Business.GetVideoByFileName(fileName) == null) { // Update database. Media DbItem = Business.GetVideoById(item.MediaId.Value); DbItem.FileName = fileName; Business.Save(); // Update in-memory list. if (!item.FileExists) { item.FileExists = true; item.FileName = fileName; playlist.RemoveAll(v => v.FileName == fileName && v.IsInDatabase == false); } else if (!item.IsInDatabase) { item.IsInDatabase = true; item.FileName = fileName; playlist.RemoveAll(v => v.FileName == fileName && v.FileExists == false); } return(true); } else { return(false); } }
private void FixContainer(VideoListItem item) { SetStatus(item, VideoListItemStatusEnum.Converting); string SrcFile = Settings.NaturalGroundingFolder + item.FileName; if (item.FileName != null && File.Exists(SrcFile)) { FFmpegProcess FileInfo = MediaInfo.GetFileInfo(SrcFile); string Ext1 = Path.GetExtension(item.FileName).ToLower(); string Ext2 = DownloadBusiness.GetFinalExtension(FileInfo.VideoStream?.Format, FileInfo.AudioStream?.Format); if ((Ext2 == ".mp4" || Ext2 == ".webm") && Ext1 != Ext2) { string DstFile = item.FileName.Substring(0, item.FileName.Length - Ext1.Length) + Ext2; if (MediaMuxer.Muxe(SrcFile, SrcFile, Settings.NaturalGroundingFolder + DstFile) == CompletionStatus.Success) { FileOperationAPIWrapper.MoveToRecycleBin(SrcFile); // Change database binding. EditVideoBusiness Business = new EditVideoBusiness(); Media ExistingData = Business.GetVideoById(item.MediaId.Value); if (ExistingData != null) { // Edit video info. ExistingData.FileName = DstFile; Business.Save(); SetStatus(item, VideoListItemStatusEnum.Done); return; } } } } SetStatus(item, VideoListItemStatusEnum.Failed); }
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); }
/// <summary> /// Moves specified video to specified location. /// </summary> /// <param name="item">The information about the move.</param> /// <param name="save">Whether to save changes to the database.</param> /// <returns>True if the move was successful, otherwise False.</returns> public bool MoveFile(MoveFileItem item, bool save) { try { Directory.CreateDirectory(Path.GetDirectoryName(Settings.NaturalGroundingFolder + item.NewFileName)); File.Move(Settings.NaturalGroundingFolder + item.FileName, Settings.NaturalGroundingFolder + item.NewFileName); if (save) { EditVideoBusiness Business = new EditVideoBusiness(); Media EditVideo = Business.GetVideoById(item.VideoId); EditVideo.FileName = item.NewFileName; Business.Save(); } // Delete source folder if empty. string SourceDirectory = Path.GetDirectoryName(Settings.NaturalGroundingFolder + item.FileName); if (!Directory.EnumerateFileSystemEntries(SourceDirectory).Any()) { Directory.Delete(SourceDirectory); } return(true); } catch { return(false); } }
public bool UpdateFileName(VideoListItem item, string fileName) { EditVideoBusiness Business = new EditVideoBusiness(); if (Business.GetVideoByFileName(fileName) == null) { // Update database. Media DbItem = Business.GetVideoById(item.MediaId.Value); DbItem.FileName = fileName; Business.Save(); // Update in-memory list. if (!item.FileExists) { item.FileExists = true; item.FileName = fileName; playlist.RemoveAll(v => v.FileName == fileName && v.IsInDatabase == false); } else if (!item.IsInDatabase) { item.IsInDatabase = true; item.FileName = fileName; playlist.RemoveAll(v => v.FileName == fileName && v.FileExists == false); } return true; } else return false; }
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; }
/// <summary> /// Moves specified video to specified location. /// </summary> /// <param name="item">The information about the move.</param> /// <param name="save">Whether to save changes to the database.</param> /// <returns>True if the move was successful, otherwise False.</returns> public bool MoveFile(MoveFileItem item, bool save) { try { Directory.CreateDirectory(Path.GetDirectoryName(Settings.NaturalGroundingFolder + item.NewFileName)); File.Move(Settings.NaturalGroundingFolder + item.FileName, Settings.NaturalGroundingFolder + item.NewFileName); if (save) { EditVideoBusiness Business = new EditVideoBusiness(); Media EditVideo = Business.GetVideoById(item.VideoId); EditVideo.FileName = item.NewFileName; Business.Save(); } // Delete source folder if empty. string SourceDirectory = Path.GetDirectoryName(Settings.NaturalGroundingFolder + item.FileName); if (!Directory.EnumerateFileSystemEntries(SourceDirectory).Any()) Directory.Delete(SourceDirectory); return true; } catch { return false; } }