Skip to content

Commit

Permalink
Add RemoveClosedCaptions options, remux away track errors, explicitly…
Browse files Browse the repository at this point in the history
… set MkvMerge IETF options
  • Loading branch information
ptr727 committed Mar 24, 2023
1 parent e89a191 commit 00217ab
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 449 deletions.
10 changes: 6 additions & 4 deletions PlexCleaner.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
".ass",
".vtt"
],
// Enable re-mux
// Enable re-mux to MKV of non-MKV files
"ReMux": true,
// File extensions to remux to MKV
"ReMuxExtensions": [
Expand All @@ -71,7 +71,7 @@
// Enable deinterlace of interlaced media
// Interlace detection is not absolute and uses interlaced frame counting
"DeInterlace": true,
// Enable re-encode
// Enable re-encode of audio or video tracks as specified in ReEncodeVideo and ReEncodeAudioFormats
"ReEncode": true,
// Re-encode the video if the Format, Codec, and Profile values match
// Empty fields will match with any value
Expand Down Expand Up @@ -136,7 +136,7 @@
"DefaultLanguage": "en",
// Enable removing of unwanted language tracks
"RemoveUnwantedLanguageTracks": true,
// Track languages to keep
// Track language matched tags to keep
// Use RFC 5646 format, see https://www.w3.org/International/articles/language-tags/
"KeepLanguages": [
"en",
Expand All @@ -162,11 +162,13 @@
// Enable removing of all tags from the media file
// Track title information is not removed
"RemoveTags": true,
// Enable removing of EIA-608 Closed Captions embedded in video streams
"RemoveClosedCaptions": true,
// Speedup media re-processing by saving media info and processed state in sidecar files
"UseSidecarFiles": true,
// Invalidate sidecar files when tool versions change
"SidecarUpdateOnToolChange": false,
// Enable verify
// Enable verification of media stream content
"Verify": true,
// Restore media file modified timestamp to original pre-processed value
"RestoreFileTimestamp": false,
Expand Down
4 changes: 4 additions & 0 deletions PlexCleaner.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
"KeepOriginalLanguage": {
"type": "boolean"
},
"RemoveClosedCaptions": {
"type": "boolean"
},
"KeepExtensions": {
"type": "array",
"items": {
Expand Down Expand Up @@ -192,6 +195,7 @@
},
"required": [
"KeepOriginalLanguage",
"RemoveClosedCaptions",
"KeepExtensions",
"ReMuxExtensions",
"ReEncodeVideo",
Expand Down
5 changes: 0 additions & 5 deletions PlexCleaner/BitrateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public class BitrateInfo
{
public void Calculate(List<Packet> packetList, int videoStream, int audioStream, int threshold)
{
if (packetList == null)
{
throw new ArgumentNullException(nameof(packetList));
}

// ShouldCompute() must not allow any Assert() to fail

// Calculate the media playback duration from timestamps
Expand Down
2 changes: 1 addition & 1 deletion PlexCleaner/ConfigFileJsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static ConfigFileJsonSchema FromJson(string json)
return JsonConvert.DeserializeObject<ConfigFileJsonSchema>(json, Settings);
// Unknown version
default:
throw new NotSupportedException(nameof(configFileJsonSchemaBase.SchemaVersion));
throw new NotImplementedException();
}
}

Expand Down
102 changes: 16 additions & 86 deletions PlexCleaner/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,11 @@ public static class Convert
public static bool ConvertToMkv(string inputName, out string outputName)
{
// Convert all tracks
return ConvertToMkv(inputName, null, null, out outputName);
return ConvertToMkv(inputName, null, out outputName);
}

public static bool ConvertToMkv(string inputName, MediaInfo keep, MediaInfo reencode, out string outputName)
public static bool ConvertToMkv(string inputName, SelectMediaInfo selectMediaInfo, out string outputName)
{
return ConvertToMkvFfMpeg(inputName, keep, reencode, out outputName);
}

public static bool ConvertToMkvFfMpeg(string inputName, out string outputName)
{
// Convert all tracks
return ConvertToMkvFfMpeg(inputName, null, null, out outputName);
}

public static bool ConvertToMkvFfMpeg(string inputName, MediaInfo keep, MediaInfo reencode, out string outputName)
{
if (inputName == null)
{
throw new ArgumentNullException(nameof(inputName));
}

// Match the logic in ReMuxToMKV()

// Test
Expand All @@ -46,8 +30,13 @@ public static bool ConvertToMkvFfMpeg(string inputName, MediaInfo keep, MediaInf
string tempName = Path.ChangeExtension(inputName, ".tmp");

// Convert using ffmpeg
// Selected is ReEncode
// NotSelected is Keep
Log.Logger.Information("ReEncode using FfMpeg : {FileName}", inputName);
if (!Tools.FfMpeg.ConvertToMkv(inputName, keep, reencode, tempName))
var result = selectMediaInfo == null ?
Tools.FfMpeg.ConvertToMkv(inputName, tempName) :
Tools.FfMpeg.ConvertToMkv(inputName, selectMediaInfo, tempName);
if (!result)
{
Log.Logger.Error("ReEncode using FfMpeg failed : {FileName}", inputName);
FileEx.DeleteFile(tempName);
Expand All @@ -67,11 +56,6 @@ public static bool ConvertToMkvFfMpeg(string inputName, MediaInfo keep, MediaInf

public static bool ReMuxToMkv(string inputName, out string outputName)
{
if (inputName == null)
{
throw new ArgumentNullException(nameof(inputName));
}

// Match the logic in ConvertToMKV()

// Test
Expand Down Expand Up @@ -135,20 +119,15 @@ public static bool ReMuxToMkv(string inputName, out string outputName)
FileEx.DeleteFile(inputName);
}

public static bool ReMuxToMkv(string inputName, MediaInfo keep, out string outputName)
public static bool ReMuxToMkv(string inputName, SelectMediaInfo selectMediaInfo, out string outputName)
{
if (inputName == null)
{
throw new ArgumentNullException(nameof(inputName));
}

if (keep == null)
{
throw new ArgumentNullException(nameof(keep));
if (selectMediaInfo == null)
{
return ReMuxToMkv(inputName, out outputName);
}

// This only works on MKV files and MkvMerge MediaInfo types
Debug.Assert(keep.Parser == MediaTool.ToolType.MkvMerge);
Debug.Assert(selectMediaInfo.Selected.Parser == MediaTool.ToolType.MkvMerge);
Debug.Assert(MkvMergeTool.IsMkvFile(inputName));

// Match the logic in ConvertToMKV()
Expand All @@ -165,8 +144,10 @@ public static bool ReMuxToMkv(string inputName, MediaInfo keep, out string outpu
string tempName = Path.ChangeExtension(inputName, ".tmp");

// Remux keeping specific tracks
// Selected is Keep
// NotSelected is Remove
Log.Logger.Information("ReMux using MkvMerge : {FileName}", inputName);
if (!Tools.MkvMerge.ReMuxToMkv(inputName, keep, tempName))
if (!Tools.MkvMerge.ReMuxToMkv(inputName, selectMediaInfo, tempName))
{
Log.Logger.Error("ReMux using MkvMerge failed : {FileName}", inputName);
FileEx.DeleteFile(tempName);
Expand All @@ -186,17 +167,6 @@ public static bool ReMuxToMkv(string inputName, MediaInfo keep, out string outpu

public static bool DeInterlaceToMkv(string inputName, out string outputName)
{
// HandBrake produces the best deinterlacing results
return DeInterlaceToMkvHandbrake(inputName, out outputName);
}

public static bool DeInterlaceToMkvHandbrake(string inputName, out string outputName)
{
if (inputName == null)
{
throw new ArgumentNullException(nameof(inputName));
}

// Match the logic in ConvertToMKV()

// Test
Expand Down Expand Up @@ -229,44 +199,4 @@ public static bool DeInterlaceToMkvHandbrake(string inputName, out string output
return inputName.Equals(outputName, StringComparison.OrdinalIgnoreCase) ||
FileEx.DeleteFile(inputName);
}

public static bool ConvertToMkvHandBrake(string inputName, out string outputName)
{
if (inputName == null)
{
throw new ArgumentNullException(nameof(inputName));
}

// Match the logic in ConvertToMKV()

// Test
if (Program.Options.TestNoModify)
{
outputName = inputName;
return true;
}

// Create a temp filename based on the input name
outputName = Path.ChangeExtension(inputName, ".mkv");
string tempName = Path.ChangeExtension(inputName, ".tmp");

// Re-encode audio and video using handbrake
Log.Logger.Information("ReEncode using HandBrake : {FileName}", inputName);
if (!Tools.HandBrake.ConvertToMkv(inputName, tempName, true, false))
{
Log.Logger.Error("ReEncode using HandBrake failed : {FileName}", inputName);
FileEx.DeleteFile(tempName);
return false;
}

// Rename the temp file to the output file
if (!FileEx.RenameFile(tempName, outputName))
{
return false;
}

// If the input and output names are not the same, delete the input
return inputName.Equals(outputName, StringComparison.OrdinalIgnoreCase) ||
FileEx.DeleteFile(inputName);
}
}
8 changes: 1 addition & 7 deletions PlexCleaner/FfMpegIdetInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;

namespace PlexCleaner;

Expand Down Expand Up @@ -56,11 +55,6 @@ public bool IsInterlaced()

public static bool GetIdetInfo(FileInfo mediaFile, out FfMpegIdetInfo idetInfo)
{
if (mediaFile == null)
{
throw new ArgumentNullException(nameof(mediaFile));
}

return Tools.FfMpeg.GetIdetInfo(mediaFile.FullName, out idetInfo);
}

Expand Down
Loading

0 comments on commit 00217ab

Please sign in to comment.