using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Core
{
public static class SceneChangeHelper
{
private static string GetSceneChangesFileName(string videoFileName)
{
var dir = Configuration.SceneChangesFolder.TrimEnd(Path.DirectorySeparatorChar);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
var file = new FileInfo(videoFileName);
var newFileName = Utilities.Sha256Hash(file.Name + file.Length + file.CreationTimeUtc.ToShortDateString()) + ".scenechanges";
newFileName = newFileName.Replace("=", string.Empty).Replace("/", string.Empty).Replace(",", string.Empty).Replace("?", string.Empty).Replace("*", string.Empty).Replace("+", string.Empty).Replace("\\", string.Empty);
newFileName = Path.Combine(dir, newFileName);
return newFileName;
}
///
/// Load scene changes from file
///
/// Video file name
/// List of scene changes in seconds
public static List FromDisk(string videoFileName)
{
var list = new List();
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;
}
///
/// Saves scene changes
///
/// Video file name
/// List of scene changes in seconds
public static void SaveSceneChanges(string videoFileName, List list)
{
var sb = new StringBuilder();
foreach (var d in list)
{
sb.AppendLine(d.ToString(CultureInfo.InvariantCulture));
}
File.WriteAllText(GetSceneChangesFileName(videoFileName), sb.ToString().Trim());
}
///
/// Delete scene changes file associated with video file
///
/// Video file name
public static void DeleteSceneChanges(string videoFileName)
{
var sceneChangesFileName = GetSceneChangesFileName(videoFileName);
if (File.Exists(sceneChangesFileName))
File.Delete(sceneChangesFileName);
}
}
}