예제 #1
0
        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);
        }
예제 #2
0
        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);
            }
        }
        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);
        }
 public async Task DownloadAndPlaySample(string artist, string title) {
     EditVideoBusiness FindVideoBusiness = new EditVideoBusiness();
     Media sample = FindVideoBusiness.GetVideoByTitle(MediaType.Video, artist, title);
     if (sample != null) {
         if (sample.FileName != null && File.Exists(Settings.NaturalGroundingFolder + sample.FileName)) {
             // File exists, play.
             await PlayVideo(sample);
         } else if (sample.DownloadUrl != null) {
             // File doesn't exist, download.
             // It will only auto-play if user is still on the same page.
             downloadPage = currentPage;
             await SessionCore.Instance.Business.DownloadManager.DownloadVideoAsync(sample, -1, DownloadBusiness_DownloadCompleted);
         }
     }
 }
예제 #5
0
        /// <summary>
        /// Merges the list of files in the folder with the data coming from the database.
        /// </summary>
        /// <param name="settings">The filters to apply to the data.</param>
        private void MergePlaylist(SearchSettings settings)
        {
            VideoListItem ListItem;
            MediaType     ItemType;

            foreach (LocalFileInfo item in files)
            {
                ItemType = EditVideoBusiness.GetFileType(item.FileName);
                ListItem = playlist.Where(v => string.Equals(v.FileName, item.FileName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (ListItem != null)
                {
                    ListItem.FileExists = true;
                }
                else
                {
                    // Add file if it matches search settings.
                    if (!item.IsInDatabase && OrphanMatchesConditions(ItemType, item.FileName, settings))
                    {
                        // Add file to ListView.
                        playlist.Add(new VideoListItem()
                        {
                            MediaId      = null,
                            MediaType    = ItemType,
                            Title        = item.FileName,
                            FileName     = item.FileName,
                            FileExists   = true,
                            IsInDatabase = false
                        });
                    }
                }
            }

            // Apply FileExists search filter.
            SearchConditionSetting Cond = settings.ConditionFilters.FirstOrDefault(f => f.Field == FieldConditionEnum.FileExists && f.Value != BoolConditionEnum.None);

            if (Cond != null)
            {
                playlist.RemoveAll(v => v.FileExists == (Cond.Value == BoolConditionEnum.No));
            }
            // Apply IsInDatabase search filter.
            if (settings.IsInDatabase == false)
            {
                playlist.RemoveAll(v => v.IsInDatabase);
            }
        }
        public void FinalizeReplace(EncodingCompletedEventArgs jobInfo) {
            EditVideoBusiness EditBusiness = new EditVideoBusiness();
            string RelativePath = jobInfo.OldFileName.StartsWith(Settings.NaturalGroundingFolder) ? jobInfo.OldFileName.Substring(Settings.NaturalGroundingFolder.Length) : jobInfo.OldFileName;
            Media EditVideo = EditBusiness.GetVideoByFileName(RelativePath);
            System.Threading.Thread.Sleep(200); // Give MPC time to release the file.
            string OriginalPath = Path.Combine(Path.GetDirectoryName(jobInfo.OldFileName), "Original", Path.GetFileName(jobInfo.OldFileName));
            string NewPath = Path.ChangeExtension(jobInfo.OldFileName, null) + Path.GetExtension(jobInfo.Settings.FinalFile);
            Directory.CreateDirectory(Path.GetDirectoryName(OriginalPath));
            PathManager.SafeMove(jobInfo.OldFileName, OriginalPath);
            PathManager.SafeMove(jobInfo.Settings.FinalFile, NewPath);
            jobInfo.Settings.FilePath = OriginalPath.Substring(Settings.NaturalGroundingFolder.Length);

            if (EditVideo != null) {
                EditVideo.FileName = NewPath.Substring(Settings.NaturalGroundingFolder.Length);
                EditVideo.Height = null;
                EditBusiness.Save();
            }
        }
예제 #7
0
 /// <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;
     }
 }
        public void FinalizeReplace(EncodingCompletedEventArgs jobInfo) {
            EditVideoBusiness EditBusiness = new EditVideoBusiness();
            Media EditVideo = EditBusiness.GetVideoByFileName(jobInfo.OldFileName);
            System.Threading.Thread.Sleep(200); // Give MPC time to release the file.
            string OriginalPath = Path.Combine(Path.GetDirectoryName(Settings.NaturalGroundingFolder + jobInfo.OldFileName), "Original", Path.GetFileName(jobInfo.OldFileName));
            string NewPath = Path.Combine(Path.GetDirectoryName(Settings.NaturalGroundingFolder + jobInfo.OldFileName), Path.GetFileNameWithoutExtension(jobInfo.OldFileName) + Path.GetExtension(jobInfo.Settings.FinalFile));
            Directory.CreateDirectory(Path.GetDirectoryName(OriginalPath));
            SafeMove(Settings.NaturalGroundingFolder + jobInfo.OldFileName, OriginalPath);
            SafeMove(jobInfo.Settings.FinalFile, NewPath);
            jobInfo.Settings.FileName = OriginalPath.Substring(Settings.NaturalGroundingFolder.Length);

            if (EditVideo != null) {
                EditVideo.FileName = NewPath.Substring(Settings.NaturalGroundingFolder.Length);
                EditVideo.Height = null;
                EditBusiness.Save();
            }
        }