Browse Source

Small tool for sanitizing tracklists and cleanup output

spesk1 4 years ago
parent
commit
2972c16c88
3 changed files with 87 additions and 5 deletions
  1. 18 1
      README.md
  2. 41 0
      sanitize_tl.pl
  3. 28 4
      yt-track-split

+ 18 - 1
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.

+ 41 - 0
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";
+
+}
+	
+

+ 28 - 4
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]);
 
 }