fixes, errors handling

This commit is contained in:
2024-03-10 01:52:23 +01:00
Unverified
parent d73ce6a05b
commit f0894c0ccd
5 changed files with 83 additions and 27 deletions

View File

@@ -117,6 +117,21 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="DialogErrorMessageNoInternetConnection" xml:space="preserve">
<value>No internet connection. Try again later.</value>
</data>
<data name="DialogErrorTitle" xml:space="preserve">
<value>Error</value>
</data>
<data name="ErrorDownloadingTimeout" xml:space="preserve">
<value>Downloading timeout. Check your internet connection and try again later.</value>
</data>
<data name="ErrorFFmpeg" xml:space="preserve">
<value>FFmpeg exited with error.</value>
</data>
<data name="ErrorFFmpegPath" xml:space="preserve">
<value>No FFmpeg executables found. Check path in settings and try again.</value>
</data>
<data name="StatusCancelled.Text" xml:space="preserve">
<value>Cancelled</value>
</data>

View File

@@ -13,6 +13,9 @@ using System.IO;
using VDownload.Services.UI.Notifications;
using VDownload.Services.UI.StringResources;
using System.Collections.Generic;
using System.Net.Http;
using Instances.Exceptions;
using FFMpegCore.Exceptions;
namespace VDownload.Core.Tasks
{
@@ -140,7 +143,11 @@ namespace VDownload.Core.Tasks
await _settingsService.Load();
string tempDirectory = $"{_settingsService.Data.Common.Temp.Directory}\\{_configurationService.Common.Path.Temp.TasksDirectory}\\{Id}";
string tempDirectory = $"{_settingsService.Data.Common.Temp.Directory}\\{_configurationService.Common.Path.Temp.TasksDirectory}\\{Guid.NewGuid()}";
if (Directory.Exists(tempDirectory))
{
Directory.Delete(tempDirectory, true);
}
Directory.CreateDirectory(tempDirectory);
List<string> content = new List<string>()
@@ -180,10 +187,11 @@ namespace VDownload.Core.Tasks
.JoinProgressReporter(onProgressProcessing, downloadResult.NewDuration);
await ffmpegBuilder.RunAsync(downloadResult.File, outputPath);
StorageFile outputFile = await StorageFile.GetFileFromPathAsync(outputPath);
UpdateStatusWithDispatcher(DownloadTaskStatus.Finalizing);
string destination = $"{DownloadOptions.Directory}\\{DownloadOptions.Filename}.{DownloadOptions.Extension}";
File.Copy(outputPath, destination, true);
await Finalizing(outputFile);
UpdateStatusWithDispatcher(DownloadTaskStatus.EndedSuccessfully);
@@ -199,7 +207,26 @@ namespace VDownload.Core.Tasks
}
catch (Exception ex)
{
UpdateErrorWithDispatcher(ex.Message);
string message;
if (ex is TaskCanceledException || ex is HttpRequestException)
{
message = _stringResourcesService.HomeDownloadsViewResources.Get("ErrorDownloadingTimeout");
}
else if (ex is InstanceFileNotFoundException)
{
message = _stringResourcesService.HomeDownloadsViewResources.Get("ErrorFFmpegPath");
}
else if (ex is FFMpegException)
{
message = _stringResourcesService.HomeDownloadsViewResources.Get("ErrorFFmpeg");
}
else
{
message = ex.Message;
}
UpdateErrorWithDispatcher(message);
UpdateStatusWithDispatcher(DownloadTaskStatus.EndedUnsuccessfully);
if (_settingsService.Data.Common.Notifications.OnUnsuccessful)
@@ -218,11 +245,19 @@ namespace VDownload.Core.Tasks
}
}
private void UpdateStatusWithDispatcher(DownloadTaskStatus status) => _dispatcherQueue.TryEnqueue(() => Status = status);
protected async Task Finalizing(StorageFile outputFile)
{
StorageFolder destination = await StorageFolder.GetFolderFromPathAsync(DownloadOptions.Directory);
private void UpdateProgressWithDispatcher(double progress) => _dispatcherQueue.TryEnqueue(() => Progress = progress);
string filename = $"{DownloadOptions.Filename}.{DownloadOptions.Extension}";
await outputFile.CopyAsync(destination, filename, NameCollisionOption.ReplaceExisting);
}
private void UpdateErrorWithDispatcher(string message) => _dispatcherQueue.TryEnqueue(() => Error = message);
protected void UpdateStatusWithDispatcher(DownloadTaskStatus status) => _dispatcherQueue.TryEnqueue(() => Status = status);
protected void UpdateProgressWithDispatcher(double progress) => _dispatcherQueue.TryEnqueue(() => Progress = progress);
protected void UpdateErrorWithDispatcher(string message) => _dispatcherQueue.TryEnqueue(() => Error = message);
#endregion
}

View File

@@ -68,6 +68,14 @@ namespace VDownload.Core.ViewModels.Home
];
if (idleStatuses.Contains(task.Status))
{
if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
string title = _stringResourcesService.HomeDownloadsViewResources.Get("DialogErrorTitle");
string message = _stringResourcesService.HomeDownloadsViewResources.Get("DialogErrorMessageNoInternetConnection");
await _dialogsService.ShowOk(title, message);
return;
}
bool continueEnqueue = true;
if (NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection)
{

View File

@@ -40,6 +40,9 @@ namespace VDownload.Services.Utility.FFmpeg
protected string _inputFile;
protected string _outputFile;
protected string _audioCodec;
protected string _videoCodec;
protected FFOptions _options;
#endregion
@@ -103,6 +106,10 @@ namespace VDownload.Services.Utility.FFmpeg
_inputFile = inputFile;
_outputFile = outputFile;
IMediaAnalysis analysis = await FFProbe.AnalyseAsync(_inputFile, _options);
_audioCodec = analysis.AudioStreams.First().CodecName;
_videoCodec = analysis.VideoStreams.First().CodecName;
FFMpegArgumentProcessor ffmpegArguments = FFMpegArguments.FromFileInput(inputFile, true, async (options) => await BuildInputArgumentOptions(options))
.OutputToFile(outputFile, true, async (options) => await BuildOutputArgumentOptions(options));
@@ -154,17 +161,13 @@ namespace VDownload.Services.Utility.FFmpeg
private async Task BuildOutputArgumentOptions(FFMpegArgumentOptions options)
{
IMediaAnalysis analysis = await FFProbe.AnalyseAsync(_inputFile, _options);
string audioCodec = analysis.AudioStreams.First().CodecName;
string videoCodec = analysis.VideoStreams.First().CodecName;
string extension = Path.GetExtension(_outputFile).Replace(".", string.Empty);
Data.Configuration.Models.Muxer muxer = _configurationService.Common.Processing.Muxers.First(x => x.Extension == extension);
if (_mediaType != MediaType.OnlyAudio)
{
IEnumerable<string> availableCodecs = muxer.VideoCodecs;
string selectedCodec = availableCodecs.Contains(videoCodec) ? "copy" : availableCodecs.First();
string selectedCodec = availableCodecs.Contains(_videoCodec) ? "copy" : availableCodecs.First();
options.WithCustomArgument($"-vcodec {selectedCodec}");
}
else
@@ -175,7 +178,7 @@ namespace VDownload.Services.Utility.FFmpeg
if (_mediaType != MediaType.OnlyVideo)
{
IEnumerable<string> availableCodecs = muxer.AudioCodecs;
string selectedCodec = availableCodecs.Contains(audioCodec) ? "copy" : availableCodecs.First();
string selectedCodec = availableCodecs.Contains(_audioCodec) ? "copy" : availableCodecs.First();
options.WithCustomArgument($"-acodec {selectedCodec}");
}
else

View File

@@ -145,15 +145,17 @@ namespace VDownload.Sources.Twitch.Models
{
token.ThrowIfCancellationRequested();
using (FileStream inputStream = File.OpenRead(path))
using (FileStream inputStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
inputStream.CopyTo(outputStream);
}
}
}
foreach (string item in sourceFiles)
{
if (deleteSource)
{
File.Delete(path);
}
File.Delete(item);
}
}
}
@@ -184,10 +186,7 @@ namespace VDownload.Sources.Twitch.Models
int retriesCount = 0;
while (true)
{
if (token.IsCancellationRequested)
{
return;
}
token.ThrowIfCancellationRequested();
try
{
byte[] data = await _httpClient.GetByteArrayAsync(chunk.Url, token);
@@ -195,10 +194,6 @@ namespace VDownload.Sources.Twitch.Models
onTaskEndSuccessfully.Invoke();
return;
}
catch (OperationCanceledException)
{
return;
}
catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
{
if (_settingsService.Data.Twitch.Vod.ChunkDownloadingError.Retry && retriesCount < _settingsService.Data.Twitch.Vod.ChunkDownloadingError.RetriesCount)