diff options
Diffstat (limited to 'win/CS/HandBrakeWPF/Helpers/AutoNameHelper.cs')
-rw-r--r-- | win/CS/HandBrakeWPF/Helpers/AutoNameHelper.cs | 261 |
1 files changed, 130 insertions, 131 deletions
diff --git a/win/CS/HandBrakeWPF/Helpers/AutoNameHelper.cs b/win/CS/HandBrakeWPF/Helpers/AutoNameHelper.cs index bcd249798..4870f54ec 100644 --- a/win/CS/HandBrakeWPF/Helpers/AutoNameHelper.cs +++ b/win/CS/HandBrakeWPF/Helpers/AutoNameHelper.cs @@ -10,11 +10,8 @@ namespace HandBrakeWPF.Helpers
{
using System;
- using System.Diagnostics;
using System.IO;
using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
using Caliburn.Micro;
@@ -22,7 +19,6 @@ namespace HandBrakeWPF.Helpers using HandBrakeWPF.Extensions;
using HandBrakeWPF.Model.Options;
- using HandBrakeWPF.Services.Encode.Model;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Services.Presets.Model;
@@ -38,18 +34,6 @@ namespace HandBrakeWPF.Helpers /// Function which generates the filename and path automatically based on
/// the Source Name, DVD title and DVD Chapters
/// </summary>
- /// <param name="task">
- /// The task.
- /// </param>
- /// <param name="sourceOrLabelName">
- /// The Source or Label Name
- /// </param>
- /// <param name="presetName">
- /// The preset Name.
- /// </param>
- /// <returns>
- /// The Generated FileName
- /// </returns>
public static string AutoName(EncodeTask task, string sourceOrLabelName, Preset presetName)
{
IUserSettingService userSettingService = IoC.Get<IUserSettingService>();
@@ -58,7 +42,6 @@ namespace HandBrakeWPF.Helpers task.Destination = string.Empty;
}
- string autoNamePath = string.Empty;
if (task.Title != 0)
{
// Get the Source Name and remove any invalid characters
@@ -66,7 +49,9 @@ namespace HandBrakeWPF.Helpers // Remove Underscores
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.AutoNameRemoveUnderscore))
+ {
sourceName = sourceName.Replace("_", " ");
+ }
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.RemovePunctuation))
{
@@ -77,10 +62,11 @@ namespace HandBrakeWPF.Helpers // Switch to "Title Case"
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.AutoNameTitleCase))
+ {
sourceName = sourceName.ToTitleCase();
+ }
// Get the Selected Title Number
-
string dvdTitle = task.Title.ToString();
// Get the Chapter Start and Chapter End Numbers
@@ -88,7 +74,9 @@ namespace HandBrakeWPF.Helpers string chapterFinish = task.EndPoint.ToString();
string combinedChapterTag = chapterStart;
if (chapterFinish != chapterStart && chapterFinish != string.Empty)
+ {
combinedChapterTag = chapterStart + "-" + chapterFinish;
+ }
// Local method to check if we have a creation time in the meta data. If not, fall back to source file creation time.
DateTime obtainCreateDateObject()
@@ -98,19 +86,19 @@ namespace HandBrakeWPF.Helpers {
return d;
}
+
try
{
return File.GetCreationTime(task.Source);
}
catch (Exception e)
{
- if (e is UnauthorizedAccessException ||
- e is PathTooLongException ||
- e is NotSupportedException)
+ if (e is UnauthorizedAccessException || e is PathTooLongException || e is NotSupportedException)
{
// Suspect the most likely concerns trying to grab the creation date in which we would want to swallow exception.
return default(DateTime);
}
+
throw e;
}
}
@@ -120,120 +108,15 @@ namespace HandBrakeWPF.Helpers string createTime = creationDateTime.ToString("HH-mm");
/*
- * File Name
- */
- string destinationFilename;
- if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != string.Empty)
- {
- destinationFilename = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat);
- destinationFilename =
- destinationFilename
- .RegexReplace(Constants.Source, sourceName)
- .RegexReplace(Constants.Title, dvdTitle)
- .RegexReplace(Constants.Chapters, combinedChapterTag)
- .RegexReplace(Constants.Date, DateTime.Now.Date.ToShortDateString().Replace('/', '-'))
- .RegexReplace(Constants.Time, DateTime.Now.ToString("HH-mm"))
- .RegexReplace(Constants.CretaionDate, createDate)
- .RegexReplace(Constants.CreationTime, createTime);
-
- if (task.VideoEncodeRateType == VideoEncodeRateType.ConstantQuality)
- {
- destinationFilename = destinationFilename.Replace(Constants.Quality, task.Quality.ToString());
- destinationFilename = destinationFilename.Replace(Constants.Bitrate, string.Empty);
- }
- else
- {
- destinationFilename = destinationFilename.Replace(
- Constants.Bitrate,
- task.VideoBitrate.ToString());
- destinationFilename = destinationFilename.Replace(Constants.Quality, string.Empty);
- }
- }
- else
- {
- destinationFilename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;
- }
-
- /*
- * File Extension
- */
- if (task.OutputFormat == OutputFormat.Mp4)
- {
- switch (userSettingService.GetUserSetting<int>(UserSettingConstants.UseM4v, typeof(int)))
- {
- case 0: // Automatic
- destinationFilename += task.IncludeChapterMarkers || MP4Helper.RequiresM4v(task) ? ".m4v" : ".mp4";
- break;
- case 1: // Always MP4
- destinationFilename += ".mp4";
- break;
- case 2: // Always M4V
- destinationFilename += ".m4v";
- break;
- }
- }
- else if (task.OutputFormat == OutputFormat.Mkv)
- {
- destinationFilename += ".mkv";
- }
- else if (task.OutputFormat == OutputFormat.WebM)
- {
- destinationFilename += ".webm";
- }
-
- /*
- * File Destination Path
+ * Generate the full path and filename
*/
-
- // If there is an auto name path, use it...
- if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().StartsWith("{source_path}") && !string.IsNullOrEmpty(task.Source))
- {
- string savedPath = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().Replace("{source_path}\\", string.Empty).Replace("{source_path}", string.Empty);
-
- string directory = Directory.Exists(task.Source)
- ? task.Source
- : Path.GetDirectoryName(task.Source);
- string requestedPath = Path.Combine(directory, savedPath);
-
- autoNamePath = Path.Combine(requestedPath, destinationFilename);
- }
- else if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Contains("{source_folder_name}") && !string.IsNullOrEmpty(task.Source))
- {
- // Second Case: We have a Path, with "{source_folder}" in it, therefore we need to replace it with the folder name from the source.
- string path = Path.GetDirectoryName(task.Source);
- if (!string.IsNullOrEmpty(path))
- {
- string[] filesArray = path.Split(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
- string sourceFolder = filesArray[filesArray.Length - 1];
-
- autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Replace("{source_folder_name}", sourceFolder), destinationFilename);
- }
- }
- else if (!task.Destination.Contains(Path.DirectorySeparatorChar.ToString()))
- {
- // Third case: If the destination box doesn't already contain a path, make one.
- if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != string.Empty &&
- userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != "Click 'Browse' to set the default location")
- {
- autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath), destinationFilename);
- }
- else
- {
- // ...otherwise, output to the source directory
- autoNamePath = null;
- }
- }
- else
- {
- // Otherwise, use the path that is already there.
- // Use the path and change the file extension to match the previous destination
- autoNamePath = Path.Combine(Path.GetDirectoryName(task.Destination), destinationFilename);
- }
-
+ string destinationFilename = GenerateDestinationFileName(task, userSettingService, sourceName, dvdTitle, combinedChapterTag, createDate, createTime);
+ string autoNamePath = GetAutonamePath(userSettingService, task, destinationFilename);
autoNamePath = CheckAndHandleFilenameCollisions(autoNamePath, destinationFilename, task, userSettingService);
+ return autoNamePath;
}
- return autoNamePath;
+ return string.Empty;
}
/// <summary>
@@ -257,6 +140,122 @@ namespace HandBrakeWPF.Helpers Directory.Exists(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim()));
}
+ private static string GenerateDestinationFileName(EncodeTask task, IUserSettingService userSettingService, string sourceName, string dvdTitle, string combinedChapterTag, string createDate, string createTime)
+ {
+ string destinationFilename;
+ if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != string.Empty)
+ {
+ destinationFilename = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat);
+ destinationFilename =
+ destinationFilename
+ .RegexReplace(Constants.Source, sourceName)
+ .RegexReplace(Constants.Title, dvdTitle)
+ .RegexReplace(Constants.Chapters, combinedChapterTag)
+ .RegexReplace(Constants.Date, DateTime.Now.Date.ToShortDateString().Replace('/', '-'))
+ .RegexReplace(Constants.Time, DateTime.Now.ToString("HH-mm"))
+ .RegexReplace(Constants.CretaionDate, createDate)
+ .RegexReplace(Constants.CreationTime, createTime);
+
+ if (task.VideoEncodeRateType == VideoEncodeRateType.ConstantQuality)
+ {
+ destinationFilename = destinationFilename.Replace(Constants.Quality, task.Quality.ToString());
+ destinationFilename = destinationFilename.Replace(Constants.Bitrate, string.Empty);
+ }
+ else
+ {
+ destinationFilename = destinationFilename.Replace(
+ Constants.Bitrate,
+ task.VideoBitrate.ToString());
+ destinationFilename = destinationFilename.Replace(Constants.Quality, string.Empty);
+ }
+ }
+ else
+ {
+ destinationFilename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;
+ }
+
+ /*
+ * File Extension
+ */
+ if (task.OutputFormat == OutputFormat.Mp4)
+ {
+ switch (userSettingService.GetUserSetting<int>(UserSettingConstants.UseM4v, typeof(int)))
+ {
+ case 0: // Automatic
+ destinationFilename += task.IncludeChapterMarkers || MP4Helper.RequiresM4v(task) ? ".m4v" : ".mp4";
+ break;
+ case 1: // Always MP4
+ destinationFilename += ".mp4";
+ break;
+ case 2: // Always M4V
+ destinationFilename += ".m4v";
+ break;
+ }
+ }
+ else if (task.OutputFormat == OutputFormat.Mkv)
+ {
+ destinationFilename += ".mkv";
+ }
+ else if (task.OutputFormat == OutputFormat.WebM)
+ {
+ destinationFilename += ".webm";
+ }
+
+ return destinationFilename;
+ }
+
+ private static string GetAutonamePath(IUserSettingService userSettingService, EncodeTask task, string destinationFilename)
+ {
+ string autoNamePath = string.Empty;
+
+ // If there is an auto name path, use it...
+ if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().StartsWith("{source_path}") && !string.IsNullOrEmpty(task.Source))
+ {
+ string savedPath = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().Replace("{source_path}\\", string.Empty).Replace("{source_path}", string.Empty);
+
+ string directory = Directory.Exists(task.Source)
+ ? task.Source
+ : Path.GetDirectoryName(task.Source);
+ string requestedPath = Path.Combine(directory, savedPath);
+
+ autoNamePath = Path.Combine(requestedPath, destinationFilename);
+ }
+ else if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Contains("{source_folder_name}") && !string.IsNullOrEmpty(task.Source))
+ {
+ // Second Case: We have a Path, with "{source_folder}" in it, therefore we need to replace it with the folder name from the source.
+ string path = Path.GetDirectoryName(task.Source);
+ if (!string.IsNullOrEmpty(path))
+ {
+ string[] filesArray = path.Split(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
+ string sourceFolder = filesArray[filesArray.Length - 1];
+
+ autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Replace("{source_folder_name}", sourceFolder), destinationFilename);
+ }
+ }
+ else if (!task.Destination.Contains(Path.DirectorySeparatorChar.ToString()))
+ {
+ // Third case: If the destination box doesn't already contain a path, make one.
+ if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != string.Empty &&
+ userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != "Click 'Browse' to set the default location")
+ {
+ autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath), destinationFilename);
+ }
+ else
+ {
+ // ...otherwise, output to the source directory
+ autoNamePath = null;
+ }
+ }
+ else
+ {
+ // Otherwise, use the path that is already there.
+ // Use the path and change the file extension to match the previous destination
+ autoNamePath = Path.Combine(Path.GetDirectoryName(task.Destination), destinationFilename);
+ }
+
+ return autoNamePath;
+ }
+
private static string CheckAndHandleFilenameCollisions(string autoNamePath, string destinationFilename, EncodeTask task, IUserSettingService userSettingService)
{
AutonameFileCollisionBehaviour behaviour = (AutonameFileCollisionBehaviour)userSettingService.GetUserSetting<int>(UserSettingConstants.AutonameFileCollisionBehaviour, typeof(int));
|