yt-track-split 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Time::Piece;
  5. if ( ! defined $ARGV[0] || ! defined $ARGV[1] ) {
  6. print "Need to pass args\n";
  7. exit 1;
  8. }
  9. my $ffmpegBin = `which ffmpeg`;
  10. chomp $ffmpegBin;
  11. if ( ! -e $ffmpegBin ) {
  12. print "You need ffmpeg installed to use this\n";
  13. exit 1;
  14. }
  15. my $track_list_path = shift(@ARGV);
  16. my $full_file_path = shift(@ARGV);
  17. my $track_list_file = `cat $track_list_path`;
  18. chomp $track_list_file;
  19. # Key is track name - val is array ref containing start time and end time
  20. my %track_times;
  21. my %track_names;
  22. sub prepend_zeros($) {
  23. my $time = shift;
  24. if ( $time =~ m/^[0-9]{1,2}\:[0-9]{2}$/ ) {
  25. if ( length $time == 4 ) {
  26. $time = "00:0" . $time;
  27. } elsif ( length $time == 5 ) {
  28. $time = "00:" . $time;
  29. } elsif ( length $time == 7 ) {
  30. $time = "0" . $time;
  31. } elsif ( length $time == 8 ) {
  32. # Don't need to do anything
  33. } else {
  34. print "Couldn't prepend zero's for $time\n";
  35. my $len = length $time;
  36. print "length of $time is $len\n";
  37. exit 1;
  38. }
  39. }
  40. return $time;
  41. }
  42. sub remove_spaces($) {
  43. my $track_name = shift;
  44. if ( $track_name =~ m/\s/ ) {
  45. $track_name =~ s/\s/_/g;
  46. return $track_name;
  47. }
  48. return $track_name;
  49. }
  50. sub get_end_time($$) {
  51. my $start_time = shift;
  52. my $next_start_time = shift;
  53. my $end_time;
  54. if ( $next_start_time =~ m/^(.*)\:([0-9]{2})$/ ) {
  55. my $end_time_val = $2 - 1;
  56. if ( $end_time_val < 0 ) { $end_time_val = 0; }
  57. if ( length $end_time_val == 1 ) { $end_time_val = "0" . $end_time_val; }
  58. $end_time = $1 . ":" . $end_time_val;
  59. $end_time = prepend_zeros($end_time);
  60. return $end_time;
  61. } else {
  62. print "get_end_time couldn't match regex on $next_start_time - exiting\n";
  63. exit 1;
  64. }
  65. }
  66. sub get_track_length() {
  67. my $ffmpegCmd = "$ffmpegBin -i '$full_file_path' 2>&1";
  68. my $ffmpegOutput = `$ffmpegCmd`;
  69. chomp $ffmpegOutput;
  70. if ( ! defined $ffmpegOutput || $ffmpegOutput eq "" ) {
  71. print "Something went wrong with calling $ffmpegCmd\n";
  72. exit 1;
  73. }
  74. my $duration;
  75. foreach my $line ( split("\n", $ffmpegOutput) ) {
  76. if ( $line =~ m/Duration:\ (.*)\.[0-9]{2},/ ) {
  77. $duration = $1;
  78. return $duration;
  79. }
  80. }
  81. print "Couldnt get duration of $full_file_path from $ffmpegCmd\n";
  82. exit 1;
  83. }
  84. sub convert_to_mp3() {
  85. my $filename;
  86. if ( $full_file_path =~ m/^.*\/(.*)\.(mp4)$/ ) {
  87. $filename = $1 . ".mp3";
  88. } else {
  89. print "Couldn't parse relative filename out of $full_file_path\n";
  90. exit 1;
  91. }
  92. #my $ffmpegCmd = "ffmpeg -i '$full_file_path' -vn acodec libmp3lame -ac 2 -ab 160k -ar 48000 '$filename'";
  93. my $ffmpegCmd = "ffmpeg -i '$full_file_path' -vn -acodec libmp3lame -ac 2 -qscale:a 4 -ar 48000 '$filename' > /dev/null 2>&1";
  94. print "Converting $full_file_path to mp3...\n";
  95. system("$ffmpegCmd") == 0 or die "Failed to convert $full_file_path to an mp3 with $ffmpegCmd\n";
  96. return $filename;
  97. }
  98. sub run_split($$$$$) {
  99. # ffmpeg -i Ryo\ Fukui\ -\ Scenery\ 1976\ \(FULL\ ALBUM\)-Hrr3dp7zRQY.mp4 -acodec copy -ss 00:00:00 -to 00:04:15 shibz.mp4
  100. my $mp3_file = shift;
  101. my $track_number = shift;
  102. my $track_name = shift;
  103. my $start_time = shift;
  104. my $end_time = shift;
  105. # Optionally covert to different codec?
  106. my $filename = "$track_number" . "-" . "$track_name" . ".mp3";
  107. my $ffmpegCmd = "$ffmpegBin -i '$mp3_file' -acodec copy -ss $start_time -to $end_time '$filename' > /dev/null 2>&1";
  108. system("$ffmpegCmd") == 0 or die "Failed to run $ffmpegCmd\n";
  109. }
  110. foreach my $line ( split("\n",$track_list_file) ) {
  111. #if ( $line =~ m/^([0-9]{1,2})\.\ (.*)\ ([0-9]{1,2}:[0-9]{2})$/ ) {
  112. if ( $line =~ m/^([0-9]{1,2})\.\ (.*)\ (.*)$/ ) {
  113. my @times;
  114. # Start time
  115. my $start_time = prepend_zeros($3);
  116. push(@times, $start_time);
  117. $track_times{$1} = \@times;
  118. my $track_name = remove_spaces($2);
  119. $track_names{$1} = $track_name;
  120. } else {
  121. print "$line doesn't match regex, exiting\n";
  122. exit 1;
  123. }
  124. }
  125. foreach my $track_number ( sort keys %track_times ) {
  126. my $next_track_val = $track_number + 1;
  127. my $next_start_time;
  128. my $end_time;
  129. if ( defined $track_times{$next_track_val} ) {
  130. $next_start_time = $track_times{$next_track_val}[0];
  131. $end_time = get_end_time($track_times{$track_number}[0],$next_start_time);
  132. } else {
  133. $end_time = get_track_length;
  134. }
  135. push(@{$track_times{$track_number}}, $end_time);
  136. }
  137. my $mp3_file = convert_to_mp3();
  138. foreach my $track_number ( sort keys %track_times ) {
  139. print "Splitting $track_number - $track_names{$track_number} - $track_times{$track_number}[0] - $track_times{$track_number}[1]\n";
  140. run_split($mp3_file,$track_number,$track_names{$track_number},$track_times{$track_number}[0],$track_times{$track_number}[1]);
  141. }