commit 0efbecc80a6189be057dc2907d3c7e5ef07c44ac Author: imrayya Date: Wed Mar 17 11:58:53 2021 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a70a07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Got rid of the default launch arguments which I used for testing +launchSettings.json diff --git a/ConvertSRTto3DASS.sln b/ConvertSRTto3DASS.sln new file mode 100644 index 0000000..5060727 --- /dev/null +++ b/ConvertSRTto3DASS.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30523.141 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertSRTto3DASS", "ConvertSRTto3DASS\ConvertSRTto3DASS.csproj", "{7D7925DE-ADBD-4A26-B12B-8F12B68D3BFE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7D7925DE-ADBD-4A26-B12B-8F12B68D3BFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D7925DE-ADBD-4A26-B12B-8F12B68D3BFE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D7925DE-ADBD-4A26-B12B-8F12B68D3BFE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D7925DE-ADBD-4A26-B12B-8F12B68D3BFE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B5E10CFB-7432-4C4B-80FB-1B9349811932} + EndGlobalSection +EndGlobal diff --git a/ConvertSRTto3DASS/ConvertSRTto3DASS.csproj b/ConvertSRTto3DASS/ConvertSRTto3DASS.csproj new file mode 100644 index 0000000..2458beb --- /dev/null +++ b/ConvertSRTto3DASS/ConvertSRTto3DASS.csproj @@ -0,0 +1,31 @@ + + + + Exe + net46 + ConvertSRTto3DASS.Converter + false + Git + false + + + + true + + + + + True + True + Settings.settings + + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + diff --git a/ConvertSRTto3DASS/Converter.cs b/ConvertSRTto3DASS/Converter.cs new file mode 100644 index 0000000..b597ab7 --- /dev/null +++ b/ConvertSRTto3DASS/Converter.cs @@ -0,0 +1,242 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.RegularExpressions; + +//SSA/ASS specification v4+ http://www.tcax.org/docs/ass-specs.htm +//SRT (SubRip) specification https://en.wikipedia.org/wiki/SubRip + +namespace ConvertSRTto3DASS +{ + class Converter + { + + static void Main(string[] args) + { + + var extracted = ExtractSubFromSRT(args[0]); + var style = CreateStandardStyle(); + var header = CreateHeader(args[0]); + var events = ProcessSubs(extracted); + + var finished = "[Script Info]\n" + header + "\n\n[V4+ Styles]\n" + style + "\n\n[Events]\n" + events; + File.WriteAllText(Path.GetFileNameWithoutExtension(args[0]) + ".ass", finished); + } + + + //TODO: Add formatting for the string. Have to look into the SSA v4.00+ specification on how to handle it + private static string ChangeFormatting(string line) + { + return ""; + } + + + //TODO: remove this and use the above method instead where it actually uses the formatting of the file + private static Regex reg = new Regex("<.+?>|(\\r)"); //Used to remove html tags used and line breaks + private static string removeFormatting(string line) + { + var replacement = reg.Replace(line, ""); + return replacement; + + } + + + private static string ProcessSubs(List> srt) + { + string endResult = "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n"; + foreach (var events in srt) + { + var start = ConvertTimeStamp(events.Item1); + var end = ConvertTimeStamp(events.Item2); + + //First layer, for the right eye + var line = "Dialogue: " + 0 + "," + start + "," + end + ",Right,,0,0,0,," + events.Item3 + "\n"; + //Second layer, for the left eye + line += "Dialogue: " + 1 + "," + start + "," + end + ",Left,,0,0,0,," + events.Item3; + + endResult += line + "\n"; + } + return endResult; + + } + + + private static string ConvertTimeStamp(string timeStamp) + { + //Change double digit hour (for .srt) to single digit hour marker (for .ass) <- very crude + var tmp = timeStamp.Substring(1); + + //Change thousands of a second hunderdth second + tmp = tmp.Substring(0, tmp.Length - 1);//TODO: Do actual rounding + return tmp.Replace(",", "."); + } + + //TODO: Make this human readable + //TODO: Add adjustable parameters + private static string CreateStandardStyle() + { + string style = "Format: " + + "Name, " + + "Fontname, " + + "Fontsize, " + + "PrimaryColour, " + + "SecondaryColour, " + + "OutlineColour, " + + "BackColour, " + + "Bold, " + + "Italic, " + + "Underline, " + + "StrikeOut, " + + "ScaleX, " + + "ScaleY, " + + "Spacing, " + + "Angle, " + + "BorderStyle, " + + "Outline, " + + "Shadow, " + + "Alignment, " + + "MarginL, " + + "MarginR, " + + "MarginV, " + + "Encoding\n" + + + "Style: " + + "Right," + + "Arial," + + "16," + + "&Hffffff," + + "&Hffffff," + + "&H0," + + "&H0," + + "0," + + "0," + + "0," + + "0," + + "100," + + "100," + + "0," + + "0," + + "1," + + "1," + + "0," + + "2," + + "192," + + "0," + + "10," + + "0\n" + + + + "Style: " + + "Left," + + "Arial," + + "16," + + "&Hffffff," + + "&Hffffff," + + "&H0," + + "&H0," + + "0," + + "0," + + "0," + + "0," + + "100," + + "100," + + "0," + + "0," + + "1," + + "1," + + "0," + + "2," + + "0," + + "192 ," + + "10," + + "0"; + return style; + } + + //TODO: create a system where you can actually give paramaters to it + private static string CreateHeader(string file, int resY = 288, int resX = 384) + { + var name = Path.GetFileNameWithoutExtension(file); + string scriptinfo = "; Generated by ConvertSRTto3D\n" + + "Title: " + name + "\n" + + "ScriptType: v4.00+\n" + + "Collisions: Normal\n" + + "PlayResX: " + resX + "\n" + + "PlayResY: " + resY + "\n" + + "ScaledBorderAndShadow: yes"; + return scriptinfo; + } + + //start, end, text, format <- tuple format + //Note - I want to change the tuple as format bit is useless as the data is carried in the text field for both .srt and .ass + //but I could reprepose it for any positional data + private static List> ExtractSubFromSRT(string file) + { + + var converted = new List>(); + + var timestamp_start = ""; + var timestamp_end = ""; + var subtitiles = ""; + string srt = File.ReadAllText(file); + + //Which dialog we are on (sanity check) + int i = 1; + + //Whether to extract the time stamp or not + int j = 0; //TODO: Change to a bool + + int linecounter = 0; //Where we are in the .srt, meant for debugging + + foreach (string line in srt.Split('\n')) + { + linecounter++; + //Empty line assumes that the next line with event number thus previous dialog is finished and can be saved + if (line == "" | line == "\r") + { + j = 0; + converted.Add(new Tuple(timestamp_start, timestamp_end, removeFormatting(subtitiles), "")); + subtitiles = ""; + continue; + } + //Try to parse the event/dialog number + if (int.TryParse(line, out int k)) + { + //Event number doesn't match counted event number. Mismatch means something probably went wrong + if (k != i) + { + Console.Error.WriteLine("Something went wrong"); + System.Environment.Exit(-1); + } + else + { + i++; + j++; + continue; + } + } + + //If it a timestamp is expected, extract it + if (j == 1) + { + timestamp_start = line.Substring(0, 12); + timestamp_end = line.Substring(17, 12); + j++; + } + else + { + if (subtitiles == "") + { + subtitiles = line; + subtitiles.Replace("\\.r", ""); + } + else + { + subtitiles = subtitiles + "\\n" + line; + } + } + } + return converted; + } + } +} diff --git a/ConvertSRTto3DASS/Properties/Settings.Designer.cs b/ConvertSRTto3DASS/Properties/Settings.Designer.cs new file mode 100644 index 0000000..ce6f161 --- /dev/null +++ b/ConvertSRTto3DASS/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ConvertSRTto3DASS.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/ConvertSRTto3DASS/Properties/Settings.settings b/ConvertSRTto3DASS/Properties/Settings.settings new file mode 100644 index 0000000..049245f --- /dev/null +++ b/ConvertSRTto3DASS/Properties/Settings.settings @@ -0,0 +1,6 @@ + + + + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6b54e57 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 imrayya + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9ca76b --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# SRT subtitles to 3D subtitles +## The problem +I wanted watch a movie with subtitle, like I always do, but the subtitles for the movie I have was a traditonal .srt while the movie I was watching was a 3D movie which was **H**alf **S**ide **B**y **S**ide (HSBS). The subtitles in question were centered which was very disorienting to read while watching the movie. The solution? Write my own program to convert it for me + +## Convert +Currently just a command line program to convert a .srt subtitle file to a .ass subtitle file while making it compatable with the HSBS format. Either drag the SRT file on the .exe to convert the .srt file or run the command `SRTto3D.exe [insert your file name here]` and the program will convert it. Word of warning, I have yet to put in formating and the program will strip off any formatting of the srt. It will also likely fail with .srt file that contain a positional information. + +## Features +_Basically nothing right now_ +- [x] Convert a .srt to an .ass/.ssa + - [x] For HSBS media + - [ ] For **S**ide **B**y **S**ide (SBS) media + - [ ] For **O**ver **U**nder (OU) media + - [ ] For Anaglyph 3D media (red and green media) + - [ ] For traditionally media (pancake mode). + - _I suggest to just use ffmpeg for this use case_ +- [ ] Convert formatting of .srt subtitles to transfer to the converted version + - [ ] Convert positional data of .srt subtitles +- [ ] Add changes which allows custom: + - [ ] Font + - [ ] Color + - [ ] Position + - [ ] Margins + - [ ] Encoding +- [ ] Add a proper GUI +- [ ] Any form of error handling or unit testing + + +## Known Issues +For some reason that I have yet to understand, Plex Transcorder doesn't not like the converted subtitles (it has a a werid issue) but it works perfectly in VLC + +## Warning +The code is a complete and utter mess with very little comments in it. This was meant to be a private project and I haven't had the chance to clean it up yet