Skip to content

Commit

Permalink
Add ability to crop audio
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Jul 18, 2024
1 parent 80b111c commit 9e14fdc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
65 changes: 64 additions & 1 deletion downloader/Downloader/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Shapes;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Downloader.Models;
Expand Down Expand Up @@ -57,7 +58,7 @@ public MainViewModel()
var normMusicPath = $"tmpMusicNorm.{AudioFormat}";
// Just in case
if (imagePath != null && File.Exists(imagePath)) File.Delete(imagePath);
if (File.Exists(imagePath)) File.Delete(imagePath);
if (File.Exists(musicPath)) File.Delete(musicPath);
if (File.Exists(normMusicPath)) File.Delete(normMusicPath);
Expand Down Expand Up @@ -93,6 +94,31 @@ public MainViewModel()
DownloadMusic = prog;
}
var startTime = int.TryParse(StartTime, out int resStartTime) ? resStartTime : 0;
var endTime = int.TryParse(EndTime, out int resEndTime) ? resEndTime : 0;
if (startTime > 0 || endTime > 0)
{
if (endTime != 0 && endTime <= startTime)
{
throw new Exception("EndTime must either be 0 or superior at StartTime");
}
var duration = endTime == 0 ? 0 : endTime - startTime;
var durationArg = duration == 0 ? string.Empty : $" -t {duration} ";
await foreach (var prog in ExecuteAndFollowAsync(new("ffmpeg", $"-ss {startTime} {durationArg} -i {musicPath} cut_{musicPath}"), (s) =>
{
return 0f;
}))
{
CutMusic = prog;
}
File.Move($"cut_{musicPath}", musicPath, true);
}
else
{
CutMusic = 1f;
}
await foreach (var prog in ExecuteAndFollowAsync(new("ffmpeg-normalize", $"{musicPath} -pr -ext {AudioFormat} -o {normMusicPath} -c:a libmp3lame"), (_) =>
{
return 0f;
Expand Down Expand Up @@ -148,6 +174,7 @@ public MainViewModel()
{
DownloadImage = 0f;
DownloadMusic = 0f;
CutMusic = 0f;
NormalizeMusic = 0f;
var mainWindow = Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop ? desktop.MainWindow : null;
MessageBoxManager.GetMessageBoxStandard("Download failed", $"An error occurred while downloading your music: {e.Message}", icon: Icon.Error).ShowAsPopupAsync(mainWindow);
Expand Down Expand Up @@ -298,9 +325,12 @@ private void ClearAll()
AlbumName = string.Empty;
AlbumUrl = string.Empty;
SongType = string.Empty;
StartTime = string.Empty;
EndTime = string.Empty;

DownloadImage = 0f;
DownloadMusic = 0f;
CutMusic = 0f;
NormalizeMusic = 0f;

SongCount = $"{_data.Musics.Count} music found";
Expand Down Expand Up @@ -468,6 +498,17 @@ public float DownloadMusic
set => this.RaiseAndSetIfChanged(ref _downloadMusic, value);
}

private float _cutMusic;
/// <summary>
/// Cutting audio with ffmpeg
/// </summary>
public float CutMusic
{
get => _cutMusic;
set => this.RaiseAndSetIfChanged(ref _cutMusic, value);
}


private float _normalizeMusic;
/// <summary>
/// Progress of the normalization of the song
Expand All @@ -477,4 +518,26 @@ public float NormalizeMusic
get => _normalizeMusic;
set => this.RaiseAndSetIfChanged(ref _normalizeMusic, value);
}

private string _startTime = "0";
public string StartTime
{
get => _startTime;
set
{
if (string.IsNullOrEmpty(value)) value = "0";
this.RaiseAndSetIfChanged(ref _startTime, value);
}
}

private string _endTime = "0";
public string EndTime
{
get => _endTime;
set
{
if (string.IsNullOrEmpty(value)) value = "0";
this.RaiseAndSetIfChanged(ref _endTime, value);
}
}
}
23 changes: 21 additions & 2 deletions downloader/Downloader/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Downloader.ViewModels"
d:DesignHeight="450"
d:DesignHeight="500"
d:DesignWidth="800"
x:DataType="vm:MainViewModel"
mc:Ignorable="d">
Expand All @@ -22,7 +22,7 @@
<TabItem Header="YouTube Download">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="270" />
<RowDefinition Height="320" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
Expand All @@ -45,6 +45,20 @@
HorizontalAlignment="Stretch"
ItemsSource="{Binding PlaylistChoices}"
SelectedIndex="{Binding PlaylistIndex}" />
<StackPanel Orientation="Horizontal">
<NumericUpDown
Width="200"
FormatString="N0"
Minimum="0"
Value="{Binding StartTime}" />
<TextBlock Margin="5,0,5,0" VerticalAlignment="Bottom">s to</TextBlock>
<NumericUpDown
Width="200"
FormatString="N0"
Minimum="0"
Value="{Binding EndTime}" />
<TextBlock Margin="5,0,0,0" VerticalAlignment="Bottom">s</TextBlock>
</StackPanel>
<Button
HorizontalAlignment="Right"
Command="{Binding DownloadCmd}"
Expand All @@ -69,6 +83,11 @@
Height="20"
Maximum="1"
Value="{Binding DownloadMusic}" />
<TextBlock>Cut</TextBlock>
<ProgressBar
Height="20"
Maximum="1"
Value="{Binding CutMusic}" />
<TextBlock>Normalization</TextBlock>
<ProgressBar
Height="20"
Expand Down

0 comments on commit 9e14fdc

Please sign in to comment.