2972c16c88ca5ce947bfc715b465d0a294bbd917
commit 2972c16c88ca5ce947bfc715b465d0a294bbd917
Author: spesk1 <spesk@pm.me>
Date: Sat Jun 29 17:15:54 2019 -0400

Small tool for sanitizing tracklists and cleanup output

diff --git a/README.md b/README.md
index 44489f2..48752ec 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,21 @@
# yt-track-split

Brittle little script used to split long .mp4 youtube audio downloads into
-individual tracks. Depends on ffmpeg, .mp4 codec, and tracklist to match provided.
+individual tracks and convert them to .mp3s.
+
+Takes a tracklist as it's first arg and a path to a
+.mp4 as it's second arg. The second arg must be a path.
+
+```
+# Doesn't work
+yt-track-split tracklist.txt some_file
+
+# Works
+yt-track-split tracklist ./some_file
+```
+
+This is brittle but I don't really care to fix it. Work's this way
+due to lazy regexing.
+
+Depends on ffmpeg, .mp4 codec, libmp3lame, and a
+tracklist that matches provided example.
diff --git a/sanitize_tl.pl b/sanitize_tl.pl
new file mode 100755
index 0000000..c94fea9
--- /dev/null
+++ b/sanitize_tl.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+# Move time codes to end of each line
+# and clean some other stuff up if needed
+
+use strict;
+use warnings;
+
+if ( ! defined $ARGV[0] ) {
+ print "Need to pass track list\n";
+ exit 1;
+}
+
+my $file = shift(@ARGV);
+
+foreach my $line ( split("\n", `cat $file`) ) {
+ chomp $line;
+ if ( $line =~ m/([0-9]{2}:[0-9]{2})/ ) {
+ my $time_code = $1;
+ $line =~ s/ $time_code//g;
+ $line = $line . " " . $time_code;
+ }
+
+ my $sanitized_line;
+ open(my $fh, ">>", \$sanitized_line);
+ my @line_chars = split("",$line);
+ foreach my $char ( @line_chars ) {
+ if ( $char !~ m/[a-zA-Z0-9:\s\.-]/ ) {
+ $char = "";
+ print $fh "$char";
+ } else {
+ print $fh "$char";
+ }
+ }
+
+ close $fh;
+ print "$sanitized_line\n";
+
+}
+
+
diff --git a/yt-track-split b/yt-track-split
index a553962..1363887 100755
--- a/yt-track-split
+++ b/yt-track-split
@@ -103,16 +103,38 @@ sub get_track_length() {
exit 1;
}

-sub run_split($$$$) {
+sub convert_to_mp3() {
+
+ my $filename;
+ if ( $full_file_path =~ m/^.*\/(.*)\.(mp4)$/ ) {
+ $filename = $1 . ".mp3";
+ } else {
+ print "Couldn't parse relative filename out of $full_file_path\n";
+ exit 1;
+ }
+
+ #my $ffmpegCmd = "ffmpeg -i '$full_file_path' -vn acodec libmp3lame -ac 2 -ab 160k -ar 48000 '$filename'";
+ my $ffmpegCmd = "ffmpeg -i '$full_file_path' -vn -acodec libmp3lame -ac 2 -qscale:a 4 -ar 48000 '$filename' > /dev/null 2>&1";
+
+ print "Converting $full_file_path to mp3...\n";
+ system("$ffmpegCmd") == 0 or die "Failed to convert $full_file_path to an mp3 with $ffmpegCmd\n";
+
+ return $filename;
+
+}
+
+sub run_split($$$$$) {

# ffmpeg -i Ryo\ Fukui\ -\ Scenery\ 1976\ \(FULL\ ALBUM\)-Hrr3dp7zRQY.mp4 -acodec copy -ss 00:00:00 -to 00:04:15 shibz.mp4
+ my $mp3_file = shift;
my $track_number = shift;
my $track_name = shift;
my $start_time = shift;
my $end_time = shift;
# Optionally covert to different codec?
- my $filename = "$track_number" . "-" . "$track_name" . ".mp4";
- my $ffmpegCmd = "$ffmpegBin -i '$full_file_path' -acodec copy -ss $start_time -to $end_time $filename 2>&1 > /dev/null";
+ my $filename = "$track_number" . "-" . "$track_name" . ".mp3";
+ my $ffmpegCmd = "$ffmpegBin -i '$mp3_file' -acodec copy -ss $start_time -to $end_time $filename > /dev/null 2>&1";
+ print "Splitting $track_name from $mp3_file -> $start_time - $end_time\n";
system("$ffmpegCmd") == 0 or die "Failed to run $ffmpegCmd\n";

}
@@ -149,10 +171,12 @@ foreach my $track_number ( sort keys %track_times ) {

}

+my $mp3_file = convert_to_mp3();
+
foreach my $track_number ( sort keys %track_times ) {

print "Splitting $track_number - $track_names{$track_number} - $track_times{$track_number}[0] - $track_times{$track_number}[1]\n";
- run_split($track_number,$track_names{$track_number},$track_times{$track_number}[0],$track_times{$track_number}[1]);
+ run_split($mp3_file,$track_number,$track_names{$track_number},$track_times{$track_number}[0],$track_times{$track_number}[1]);

}