2016-08-04 22:41:37 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Nikse.SubtitleEdit.Core
|
|
|
|
|
{
|
|
|
|
|
public static class SceneChangeHelper
|
|
|
|
|
{
|
|
|
|
|
|
2016-08-05 17:14:03 +02:00
|
|
|
|
private static string GetSceneChangesFileName(string videoFileName)
|
2018-08-25 09:20:02 +02:00
|
|
|
|
{
|
|
|
|
|
var dir = Configuration.SceneChangesDirectory.TrimEnd(Path.DirectorySeparatorChar);
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
|
|
|
|
|
var old = GetSceneChangesFileNameOld(videoFileName); // remove old file name check in 3.5.8...
|
|
|
|
|
if (File.Exists(old))
|
|
|
|
|
return old;
|
|
|
|
|
|
|
|
|
|
var newFileName = MovieHasher.GenerateHash(videoFileName) + ".scenechanges";
|
|
|
|
|
newFileName = Path.Combine(dir, newFileName);
|
|
|
|
|
return newFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetSceneChangesFileNameOld(string videoFileName)
|
2016-08-04 22:41:37 +02:00
|
|
|
|
{
|
2016-10-31 02:21:37 +01:00
|
|
|
|
var dir = Configuration.SceneChangesDirectory.TrimEnd(Path.DirectorySeparatorChar);
|
2016-08-06 14:50:12 +02:00
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
|
|
|
|
|
var file = new FileInfo(videoFileName);
|
|
|
|
|
var newFileName = Utilities.Sha256Hash(file.Name + file.Length + file.CreationTimeUtc.ToShortDateString()) + ".scenechanges";
|
2017-12-10 22:18:13 +01:00
|
|
|
|
newFileName = newFileName.RemoveChar('=').RemoveChar('/').RemoveChar(',').RemoveChar('?').RemoveChar('*').RemoveChar('+').RemoveChar('\\');
|
2016-08-06 14:50:12 +02:00
|
|
|
|
newFileName = Path.Combine(dir, newFileName);
|
|
|
|
|
return newFileName;
|
2016-08-04 22:41:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 17:14:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Load scene changes from file
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="videoFileName">Video file name</param>
|
|
|
|
|
/// <returns>List of scene changes in seconds</returns>
|
2016-08-04 22:41:37 +02:00
|
|
|
|
public static List<double> FromDisk(string videoFileName)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<double>();
|
|
|
|
|
var sceneChangesFileName = GetSceneChangesFileName(videoFileName);
|
|
|
|
|
if (!File.Exists(sceneChangesFileName))
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
|
|
foreach (var line in File.ReadLines(sceneChangesFileName))
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(line))
|
|
|
|
|
{
|
|
|
|
|
list.Add(double.Parse(line, CultureInfo.InvariantCulture));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 17:14:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves scene changes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="videoFileName">Video file name</param>
|
|
|
|
|
/// <param name="list">List of scene changes in seconds</param>
|
|
|
|
|
public static void SaveSceneChanges(string videoFileName, List<double> list)
|
2016-08-04 22:41:37 +02:00
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var d in list)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine(d.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
}
|
|
|
|
|
File.WriteAllText(GetSceneChangesFileName(videoFileName), sb.ToString().Trim());
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 17:14:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete scene changes file associated with video file
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="videoFileName">Video file name</param>
|
|
|
|
|
public static void DeleteSceneChanges(string videoFileName)
|
2016-08-04 22:41:37 +02:00
|
|
|
|
{
|
|
|
|
|
var sceneChangesFileName = GetSceneChangesFileName(videoFileName);
|
|
|
|
|
if (File.Exists(sceneChangesFileName))
|
|
|
|
|
File.Delete(sceneChangesFileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-08-05 17:14:03 +02:00
|
|
|
|
}
|