Skip to content

Commit

Permalink
Introduce the ability to split a video's original audio without encoding
Browse files Browse the repository at this point in the history
This commit introduces the "raw" encoding mechanism, which does nothing
except split the source audio into separate tracks without any encoding
whatsoever. This option is perfect for those who prefer to simply split
the audio into separate files in the original format.

To use, simply use '--format raw' as the parameter.
  • Loading branch information
MirisWisdom committed Oct 14, 2021
1 parent 23f57d0 commit 3184658
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This project allows you to transform long YouTube album videos into properly cur
- Embedded cover art, using thumbnails derived from the provided video, or using an existing image
- Batch processing of multiple album videos from YouTube or local videos
- Song information can be read from a file, or from a YouTube video's chapters/timeline
- Optional lossless splitting of the video's original audio, without any re-encoding whatsoever

## Album Records

Expand Down Expand Up @@ -68,6 +69,10 @@ First, create the records file as described above. Once you're done, invoke the
# encoding of choice (default is mp3)
--format flac \

# losslessly split the original audio
# no metadata or re-encoding is applied
--format raw \

# mass fill with optional metadata
--genre "OP/ED/IN/IM" \
--artist "Various" --artist "Artists" \
Expand All @@ -82,7 +87,7 @@ Once you're ready, continue with the program and it will start curating the vide

| Parameter | Description |
| ----------------- | ---------------------------------------------------------------------------------------- |
| `--format=VALUE` | audio encoding format; supported values: `mp3`, `flac`, `vorbis`, `opus` |
| `--format=VALUE` | audio encoding format; supported values: `mp3`, `flac`, `vorbis`, `opus`, `raw` |
| `--album=VALUE` | path to album record file(s) (see above); multiple `--album 'abc.txt' --album 'xyz.txt'` |
| `--artist=VALUE` | album artist(s) to assign to the tracks' metadata; multiple: `--artist 'a' --artist 'b'` |
| `--genre=VALUE` | genre to assign to the tracks' metadata |
Expand Down
3 changes: 3 additions & 0 deletions doc/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ The encoding process creates an audio file of a [track](./track.md) using the ch
| FLAC | `flac` |
| Ogg Vorbis | `oggenc` |
| Ogg Opus | `opusenc` |
| RAW^0 | `ffmpeg` |

^0 = RAW refers to lossless splitting of the video's original audio stream into separate tracks, without any re-encoding whatsoever. Everything described above gets skipped when using this encoding "format".
8 changes: 5 additions & 3 deletions src/Albums/Album.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using System.Linq;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using Gunloader.Programs;
using Gunloader.Encoders;
using Gunloader.Serialisation;
using static System.Guid;
using static System.IO.Path;
Expand Down Expand Up @@ -63,16 +63,18 @@ public DirectoryInfo Target /* normalised album title // guid on empty album */

public void Encode(Toolkit toolkit)
{
var output = new FileInfo(NewGuid().ToString());
var extract = toolkit.Encoder is RAW;
var video = Source.Contains("http")
? toolkit.YTDL.Download(Source, new FileInfo(NewGuid().ToString()))
? toolkit.YTDL.Download(Source, output, extract)
: new FileInfo(Source);

if (!Target.Exists)
Target.Create();

foreach (var track in Tracks)
{
var encoded = track.Encode(toolkit, new FileInfo(Source));
var encoded = track.Encode(toolkit, video);
var final = Combine(Target.FullName, $"{track.Number}. {track.Normalised}{encoded.Extension}");

encoded.MoveTo(final);
Expand Down
52 changes: 52 additions & 0 deletions src/Encoders/RAW.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (C) 2021 Miris Wisdom
*
* This file is part of Gunloader.
*
* Gunloader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2.
*
* Gunloader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gunloader. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Diagnostics;
using System.IO;
using static System.Diagnostics.Process;
using static System.IO.Path;

namespace Gunloader.Encoders
{
public class RAW : Encoder
{
public string Program { get; set; } = "ffmpeg";

public override FileInfo Encode(Track track, FileInfo audio = null, FileInfo cover = null)
{
audio ??= new FileInfo(track.Number + ".wav");
var output = $"{GetFileNameWithoutExtension(track.Number)}{audio.Extension}";

/**
* Split input audio into separate files of the same type, without any form of re-encoding.
*/

Start(new ProcessStartInfo
{
FileName = Program,
Arguments = $"-ss {track.Start} " +
$"{(string.IsNullOrWhiteSpace(track.End) ? "" : $"-to {track.End}")} " +
$"-i {audio.Name} " +
"-c:a copy " +
$"{output}"
})?.WaitForExit();

return new FileInfo(output);
}
}
}
6 changes: 6 additions & 0 deletions src/Program.Args.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public static partial class Program
Toolkit.Encoder = new Opus();
Toolkit.FFmpeg.Lossy = true;
break;
case "raw":
case "original":
case "no-encode":
Toolkit.Encoder = new RAW();
Toolkit.FFmpeg.Lossy = false;
break;
default:
WriteLine("Unknown format provided!");
Exit(2);
Expand Down
7 changes: 4 additions & 3 deletions src/Programs/YTDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public class YTDL
{
public string Program { get; set; } = "youtube-dl";

public FileInfo Download(string download, FileInfo output)
public FileInfo Download(string download, FileInfo output, bool extract = false)
{
Start(new ProcessStartInfo
{
FileName = Program,
Arguments = $"{download} " +
$"--output {output.Name}"
Arguments = $"{download} " +
$"{(extract ? "-x --format bestaudio" : "")} " +
$"--output {output.Name}.%(ext)s"
})?.WaitForExit();

/**
Expand Down
22 changes: 16 additions & 6 deletions src/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Linq;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using Gunloader.Encoders;

namespace Gunloader
{
Expand Down Expand Up @@ -53,16 +54,25 @@ public class Track

public FileInfo Encode(Toolkit toolkit, FileInfo video)
{
var cover = toolkit.FFmpeg.ExtractCover(video, this);
var audio = toolkit.FFmpeg.ExtractAudio(video, this);
var encoded = toolkit.Encoder.Encode(this, audio, cover);
var cover = toolkit.FFmpeg.ExtractCover(video, this);
FileInfo encoded;

if (toolkit.Encoder is RAW)
{
encoded = toolkit.Encoder.Encode(this, video, cover);
}
else
{
var audio = toolkit.FFmpeg.ExtractAudio(video, this);
encoded = toolkit.Encoder.Encode(this, audio, cover);

if (audio.Exists)
audio.Delete();
}

if (cover.Exists)
cover.Delete();

if (audio.Exists)
audio.Delete();

return encoded;
}
}
Expand Down

0 comments on commit 3184658

Please sign in to comment.