Skip to content

Commit

Permalink
allow steal subtitle from other video
Browse files Browse the repository at this point in the history
  • Loading branch information
Anime4000 committed Jul 1, 2017
1 parent 24d6888 commit ca523ea
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 24 deletions.
64 changes: 45 additions & 19 deletions ifme/MediaEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ private int VideoMuxing(MediaQueue queue)

if (queue.OutputFormat == TargetFormat.MP4)
{
string cmdvideo = string.Empty;
string cmdaudio = string.Empty;
var cmdvideo = string.Empty;
var cmdaudio = string.Empty;

var mp4vid = 0;
foreach (var video in Directory.GetFiles(tempDir, "video*"))
Expand All @@ -312,31 +312,44 @@ private int VideoMuxing(MediaQueue queue)
}
else if (queue.OutputFormat == TargetFormat.MKV)
{
string cmdvideo = string.Empty;
string cmdaudio = string.Empty;
string cmdsubs = string.Empty;
string cmdattach = string.Empty;
string cmdchapter = string.Empty;
var mkvideo = string.Empty;
var mkaudio = string.Empty;
var mksubs = string.Empty;
var mkattach = string.Empty;
var mkchapter = string.Empty;

var ffvideo = string.Empty;
var ffaudio = string.Empty;
var ffsubs = string.Empty;

var tags = string.Format(Properties.Resources.MkvTags, Get.AppNameLong, Get.AppNameLib);
File.WriteAllText(Path.Combine(tempDir, "tags.xml"), tags);

foreach (var video in Directory.GetFiles(tempDir, "video*"))
cmdvideo += $"--language 0:{Get.FileLang(video)} \"{video}\" ";
{
mkvideo += $"--language 0:{Get.FileLang(video)} \"{video}\" ";
ffvideo += $"-i \"{video}\" ";
}

foreach (var audio in Directory.GetFiles(tempDir, "audio*"))
cmdaudio += $"--language 0:{Get.FileLang(audio)} \"{audio}\" ";
{
mkaudio += $"--language 0:{Get.FileLang(audio)} \"{audio}\" ";
ffaudio += $"-i \"{audio}\" ";
}

foreach (var subs in Directory.GetFiles(tempDir, "subtitle*"))
cmdsubs += $"--sub-charset 0:UTF-8 --language 0:{Get.FileLang(subs)} \"{subs}\" ";
{
mksubs += $"--sub-charset 0:UTF-8 --language 0:{Get.FileLang(subs)} \"{subs}\" ";
ffsubs += $"-i \"{subs}\" ";
}

foreach (var attach in Directory.GetFiles(Path.Combine(tempDir, "attachments"), "*.*"))
{
foreach (var item in queue.Attachment)
{
if (string.Equals(item.Name, Path.GetFileName(attach)))
{
cmdattach += $"--attachment-mime-type \"{item.Mime}\" --attachment-description yes --attach-file \"{attach}\" ";
mkattach += $"--attachment-mime-type \"{item.Mime}\" --attachment-description yes --attach-file \"{attach}\" ";
break; // save time, leave loop once found
}
}
Expand All @@ -346,11 +359,11 @@ private int VideoMuxing(MediaQueue queue)
{
FileInfo ChapLen = new FileInfo(Path.Combine(tempDir, "chapters.xml"));
if (ChapLen.Length > 256)
cmdchapter = $"--chapters \"{Path.Combine(tempDir, "chapters.xml")}\"";
mkchapter = $"--chapters \"{Path.Combine(tempDir, "chapters.xml")}\"";
}

// try
var cmd = $"{cmdvideo} {cmdaudio} {cmdsubs} {cmdattach} {cmdchapter}";
var cmd = $"{mkvideo} {mkaudio} {mksubs} {mkattach} {mkchapter}";
var exitcode = ProcessManager.Start(MkvMerge, $"-o \"{fileout}.mkv\" --disable-track-statistics-tags -t 0:\"{Path.Combine(tempDir, "tags.xml")}\" {cmd}");

if (exitcode == 0)
Expand All @@ -366,7 +379,7 @@ private int VideoMuxing(MediaQueue queue)
ConsoleEx.Write(LogLevel.Error, "MkvToolNix can't merge on first attempt, trying to skip adding tags, chapters & fonts!\n");

// try without chapter and fonts
cmd = $"{cmdvideo} {cmdaudio} {cmdsubs}";
cmd = $"{mkvideo} {mkaudio} {mksubs}";
exitcode = ProcessManager.Start(MkvMerge, $"-o \"{fileout}.mkv\" --disable-track-statistics-tags {cmd}");

if (exitcode == 0)
Expand All @@ -379,16 +392,29 @@ private int VideoMuxing(MediaQueue queue)
}
else
{
// copy whole thing
ConsoleEx.Write(LogLevel.Error, "MkvToolNix still can't merge on second attempt! BACKUP ALL RAW FILE TO SAVE FOLDER!\n");
Get.DirectoryCopy(tempDir, Path.Combine(saveDir, fileout), true);
// try with FFmpeg
ConsoleEx.Write(LogLevel.Error, "MkvToolNix still can't merge on second attempt! Using FFmpeg to merge!\n");

cmd = $"{ffvideo} {ffaudio} {ffsubs}";
exitcode = ProcessManager.Start(FFmpeg, $"{cmd} -vcodec copy -acodec copy -scodec copy -y \"{fileout}.mkv\"");

if (exitcode == 0)
{
ConsoleEx.Write(LogLevel.Normal, "FFmpeg merge perfectly, without attachment.\n");
}
else
{
// copy whole thing
ConsoleEx.Write(LogLevel.Error, "Fuck! Video encoder make a mistake! Not my fault!\n");
Get.DirectoryCopy(tempDir, Path.Combine(saveDir, fileout), true);
}
}
}
}
else if (queue.OutputFormat == TargetFormat.WEBM)
{
string cmdvideo = string.Empty;
string cmdaudio = string.Empty;
var cmdvideo = string.Empty;
var cmdaudio = string.Empty;

foreach (var video in Directory.GetFiles(tempDir, "video*"))
{
Expand Down
4 changes: 2 additions & 2 deletions ifme/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("7.3.*")]
[assembly: AssemblyFileVersion("7.3")]
[assembly: AssemblyVersion("7.4.*")]
[assembly: AssemblyFileVersion("7.4")]
10 changes: 10 additions & 0 deletions ifme/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ifme/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,7 @@
<data name="icon22_addred" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icon22_addred.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icon16_add2" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icon16_add2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added ifme/Resources/icon16_add2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions ifme/frmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion ifme/frmMain.Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,29 @@ private void SubtitleAdd(string file)
}
}

private void AttachmentAdd(string file)
private void SubtitleAdd2(string file)
{
if (lstMedia.SelectedItems.Count > 0)
{
var queue = (MediaQueue)lstMedia.SelectedItems[0].Tag;

var stream = new FFmpegDotNet.FFmpeg.Stream(file).Subtitle;

foreach (var item in stream)
{
queue.Subtitle.Add(new MediaQueueSubtitle
{
Enable = true,
File = file,
Id = item.Id,
Lang = item.Language,
Format = Get.CodecFormat(item.Codec)
});
}
}
}

private void AttachmentAdd(string file)
{
if (lstMedia.SelectedItems.Count > 0)
{
Expand Down
9 changes: 9 additions & 0 deletions ifme/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,15 @@ private void btnSubAdd_Click(object sender, EventArgs e)
UXReloadMedia();
}

private void btnSubAdd2_Click(object sender, EventArgs e)
{
if (lstMedia.SelectedItems.Count > 0)
foreach (var item in OpenFiles(MediaType.Video))
SubtitleAdd2(item);

UXReloadMedia();
}

private void btnSubDel_Click(object sender, EventArgs e)
{
if (lstMedia.SelectedItems.Count > 0)
Expand Down
3 changes: 3 additions & 0 deletions ifme/ifme.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@
<ItemGroup>
<None Include="Resources\icon22_addred.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\icon16_add2.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit ca523ea

Please sign in to comment.