Skip to content

Commit b3b1096

Browse files
committed
Merge branch 'fix/tmp-extension'
2 parents 17e64e4 + c8b944b commit b3b1096

3 files changed

Lines changed: 121 additions & 23 deletions

File tree

subs2srs/DialogPreview.cs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,8 @@ private async void OnPreviewAudio(Gtk.Button s, EventArgs e)
10201020
string wav = SysPath.Combine(_wv.MediaDir,
10211021
ConstantSettings.TempAudioPreviewFilename);
10221022

1023+
string errorMsg = null;
1024+
10231025
await Task.Run(() =>
10241026
{
10251027
try { if (File.Exists(mp3)) File.Delete(mp3); } catch { }
@@ -1043,21 +1045,73 @@ await Task.Run(() =>
10431045
|| streamNum == "-" || !streamNum.Contains(":"))
10441046
streamNum = "0:a:0";
10451047

1046-
UtilsAudio.ripAudioFromVideo(
1047-
Settings.Instance.VideoClips.Files[ep],
1048-
streamNum, st, en,
1049-
Settings.Instance.AudioClips.Bitrate, mp3, null);
1048+
try
1049+
{
1050+
var audioFormat = Settings.Instance.AudioClips.AudioFormat;
1051+
var audioCodec = audioFormat == "Opus"
1052+
? UtilsVideo.AudioCodec.Opus
1053+
: UtilsVideo.AudioCodec.MP3;
1054+
1055+
UtilsAudio.ripAudioFromVideo(
1056+
Settings.Instance.VideoClips.Files[ep],
1057+
streamNum, st, en,
1058+
Settings.Instance.AudioClips.Bitrate, mp3, null,
1059+
audioCodec);
1060+
1061+
if (!File.Exists(mp3) || new FileInfo(mp3).Length == 0)
1062+
errorMsg = "Failed to extract audio: output file not created or empty.";
1063+
}
1064+
catch (Exception ex)
1065+
{
1066+
errorMsg = "Failed to extract audio from video: " + ex.Message;
1067+
}
1068+
}
1069+
else if (Settings.Instance.AudioClips.UseExistingAudio &&
1070+
Settings.Instance.AudioClips.Files?.Length > ep)
1071+
{
1072+
try
1073+
{
1074+
string existingAudio = Settings.Instance.AudioClips.Files[ep];
1075+
UtilsAudio.cutAudio(existingAudio, st, en, mp3);
1076+
1077+
if (!File.Exists(mp3) || new FileInfo(mp3).Length == 0)
1078+
errorMsg = "Failed to cut audio: output file not created or empty.";
1079+
}
1080+
catch (Exception ex)
1081+
{
1082+
errorMsg = "Failed to cut audio from existing file: " + ex.Message;
1083+
}
1084+
}
1085+
else
1086+
{
1087+
errorMsg = "No audio source available. Check Video/Audio file settings in Preferences.";
10501088
}
10511089

1052-
if (File.Exists(mp3) && new FileInfo(mp3).Length > 0)
1053-
UtilsAudio.convertAudioFormat(mp3, wav, 2);
1090+
if (string.IsNullOrEmpty(errorMsg) &&
1091+
File.Exists(mp3) && new FileInfo(mp3).Length > 0)
1092+
{
1093+
try
1094+
{
1095+
UtilsAudio.convertAudioFormat(mp3, wav, 2);
1096+
}
1097+
catch (Exception ex)
1098+
{
1099+
errorMsg = "Failed to convert audio to WAV: " + ex.Message;
1100+
}
1101+
}
10541102
});
10551103

10561104
if (_destroyed) return;
10571105

10581106
_btnAudio.SetSensitive(true);
10591107
_btnAudio.SetLabel("Preview Audio");
10601108

1109+
if (!string.IsNullOrEmpty(errorMsg))
1110+
{
1111+
UtilsMsg.showErrMsg(errorMsg + "\n\nCheck terminal for full ffmpeg output.");
1112+
return;
1113+
}
1114+
10611115
if (File.Exists(wav))
10621116
{
10631117
try
@@ -1070,7 +1124,14 @@ await Task.Run(() =>
10701124
p.StartInfo.CreateNoWindow = true;
10711125
p.Start();
10721126
}
1073-
catch { }
1127+
catch (Exception ex)
1128+
{
1129+
UtilsMsg.showErrMsg("Failed to play audio: " + ex.Message);
1130+
}
1131+
}
1132+
else
1133+
{
1134+
UtilsMsg.showErrMsg("Audio preview file was not created. Check ffmpeg output.");
10741135
}
10751136
}
10761137

subs2srs/Settings.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ private static string FindInPath(string name)
153153

154154
public static string TempImageFilename { get; } = $"subs2srs_temp_{Guid.NewGuid()}.jpg";
155155
public static string TempVideoFilename { get; } = $"subs2srs_temp_{Guid.NewGuid()}";
156-
public static string TempAudioFilename { get; } = $"subs2srs_temp_{Guid.NewGuid()}.tmp";
156+
public static string TempAudioFilename { get; set; } = $"subs2srs_temp_{Guid.NewGuid()}.{PrefDefaults.DefaultAudioFormat.ToLower()}";
157157

158158
public static string AudioFilenameFormatWithExt { get; private set; } = PrefDefaults.AudioFilenameFormat;
159159
public static string ExtractMediaAudioFilenameFormatWithExt { get; private set; } = PrefDefaults.ExtractMediaAudioFilenameFormat;
160160

161161
public static void UpdateAudioFilenameFormats()
162162
{
163-
AudioFilenameFormatWithExt = PrefDefaults.AudioFilenameFormat.Replace(".mp3", $".{Settings.Instance.AudioClips.AudioFormat?.ToLower() ?? "mp3"}");
164-
ExtractMediaAudioFilenameFormatWithExt = PrefDefaults.ExtractMediaAudioFilenameFormat.Replace(".mp3", $".{Settings.Instance.AudioClips.AudioFormat?.ToLower() ?? "mp3"}");
163+
string ext = Settings.Instance.AudioClips.AudioFormat?.ToLower() ?? "mp3";
164+
TempAudioFilename = $"subs2srs_temp_{Guid.NewGuid()}.{ext}";
165+
AudioFilenameFormatWithExt = PrefDefaults.AudioFilenameFormat.Replace(".mp3", $".{ext}");
166+
ExtractMediaAudioFilenameFormatWithExt = PrefDefaults.ExtractMediaAudioFilenameFormat.Replace(".mp3", $".{ext}");
165167
}
166168

167169
public static string TempAudioPreviewFilename { get; } = $"subs2srs_temp_{Guid.NewGuid()}.wav";
@@ -278,7 +280,7 @@ public static int DefaultAudioClipBitrate
278280
public static string AudioFormat
279281
{
280282
get => Prefs.AudioFormat;
281-
set => Prefs.AudioFormat = value;
283+
set { Prefs.AudioFormat = value; UpdateAudioFilenameFormats(); }
282284
}
283285

284286
public static bool DefaultAudioNormalize

subs2srs/UtilsCommon.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2016 Christopher Brochtrup
1+
// Copyright (C) 2009-2016 Christopher Brochtrup
22
// Copyright (C) 2026 fkzys and contributors
33
//
44
// This file is part of subs2srs.
@@ -23,6 +23,7 @@
2323
using System.Diagnostics;
2424
using System.IO;
2525
using System.Linq;
26+
using System.Text;
2627

2728
namespace subs2srs
2829
{
@@ -173,7 +174,7 @@ private static IEnumerable<string> getFFmpegPaths()
173174
/// <summary>
174175
/// Try to call an exe with provided arguments. Returns true on success.
175176
/// </summary>
176-
private static bool callExe(string exe, string args, bool useShellExecute, bool createNoWindow)
177+
private static string? callExe(string exe, string args, bool useShellExecute, bool createNoWindow)
177178
{
178179
try
179180
{
@@ -182,11 +183,15 @@ private static bool callExe(string exe, string args, bool useShellExecute, bool
182183
process.StartInfo.Arguments = args;
183184
process.StartInfo.UseShellExecute = useShellExecute;
184185
process.StartInfo.CreateNoWindow = createNoWindow;
186+
var stderr = new StringBuilder();
185187
if (!useShellExecute)
186188
{
187189
process.StartInfo.RedirectStandardError = true;
188190
process.StartInfo.RedirectStandardOutput = true;
189-
process.ErrorDataReceived += (s, e) => { };
191+
process.ErrorDataReceived += (s, e) =>
192+
{
193+
if (e.Data != null) stderr.AppendLine(e.Data);
194+
};
190195
process.OutputDataReceived += (s, e) => { };
191196
}
192197
process.Start();
@@ -196,14 +201,36 @@ private static bool callExe(string exe, string args, bool useShellExecute, bool
196201
process.BeginOutputReadLine();
197202
}
198203
process.WaitForExit();
199-
return true;
204+
if (!useShellExecute && process.ExitCode != 0)
205+
{
206+
string full = stderr.ToString();
207+
if (full.Length > 0)
208+
Console.Error.WriteLine($"[ffmpeg stderr]\n{full}");
209+
string lastLine = GetLastNonEmptyLine(full);
210+
return $"ffmpeg exited with code {process.ExitCode}: {lastLine}";
211+
}
212+
return null;
200213
}
201-
catch
214+
catch (Exception ex)
202215
{
203-
return false;
216+
return ex.Message;
204217
}
205218
}
206219

220+
private static string GetLastNonEmptyLine(string text)
221+
{
222+
if (string.IsNullOrWhiteSpace(text))
223+
return "(no output)";
224+
var lines = text.Split('\n', StringSplitOptions.RemoveEmptyEntries);
225+
for (int i = lines.Length - 1; i >= 0; i--)
226+
{
227+
string line = lines[i].Trim();
228+
if (line.Length > 0)
229+
return line;
230+
}
231+
return "(no output)";
232+
}
233+
207234

208235
/// <summary>
209236
/// Try to call an exe and return stdout. Returns "Error." on failure.
@@ -296,24 +323,29 @@ private static bool runProcessWithProgress(string exe, string args, IProgressRep
296323

297324
/// <summary>
298325
/// Call an exe with the provided arguments, trying multiple paths.
326+
/// Returns null on success, error message on failure.
299327
/// </summary>
300-
public static void startProcess(string relExePath, string fullExePath, string args,
328+
public static string? startProcess(string relExePath, string fullExePath, string args,
301329
bool useShellExecute, bool createNoWindow)
302330
{
331+
string? lastError = null;
303332
foreach (string exe in getExePaths(relExePath, fullExePath))
304333
{
305-
if (callExe(exe, args, useShellExecute, createNoWindow))
306-
return;
334+
lastError = callExe(exe, args, useShellExecute, createNoWindow);
335+
if (lastError == null)
336+
return null;
307337
}
338+
return lastError;
308339
}
309340

310341

311342
/// <summary>
312343
/// Call an exe with the provided arguments. Don't open a window.
344+
/// Returns null on success, error message on failure.
313345
/// </summary>
314-
public static void startProcess(string relExePath, string fullExePath, string args)
346+
public static string? startProcess(string relExePath, string fullExePath, string args)
315347
{
316-
startProcess(relExePath, fullExePath, args, false, true);
348+
return startProcess(relExePath, fullExePath, args, false, true);
317349
}
318350

319351

@@ -335,11 +367,14 @@ public static string startProcessAndGetStdout(string relExePath, string fullExeP
335367

336368
/// <summary>
337369
/// Call ffmpeg with provided arguments. Blocking.
370+
/// Throws Exception on ffmpeg failure.
338371
/// </summary>
339372
public static void startFFmpeg(string ffmpegArgs, bool useShellExecute, bool createNoWindow)
340373
{
341-
startProcess(ConstantSettings.PathFFmpegExe, ConstantSettings.PathFFmpegFullExe,
374+
string? error = startProcess(ConstantSettings.PathFFmpegExe, ConstantSettings.PathFFmpegFullExe,
342375
ffmpegArgs, useShellExecute, createNoWindow);
376+
if (error != null)
377+
throw new Exception(error);
343378
}
344379

345380

0 commit comments

Comments
 (0)