- #!/usr/bin/perl
- # Copyright © 2012 Adam Nilsson
- #
- # This program 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, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- # This script decodes FLAC files into RAM up to the limit defined in
- # $disk_size. The $total variable is used to count the total amount
- # of data decoded and stored in RAM at any time.
- # The script looks for the CPU core count in /proc/cpuinfo and starts
- # as many threads of 'lame' as it finds cores. The threads are left
- # waiting for files to be enqueued.
- use 5.14.0;
- use strict;
- use warnings;
- use threads::shared;
- use Thread::Queue;
- my $total :shared = 0;
- my $disk_size = 1000000000;
- my $end :shared;
- my %decoded :shared;
- my @dirs;
- my %pcm :shared;
- my @opts = ('-q 0', '-V 2', '--silent', '--id3v2-only');
- my $q = Thread::Queue->new();
- if (!$ARGV[0] || ! -d $ARGV[0]) {
- say "Usage: " . basename($0) .
- " [FLAC directory 1] .. [FLAC directory n]";
- exit;
- }
- foreach my $arg (@ARGV) {
- my $src = abs_path($arg);
- if (-d $src) {
- getdirs($src);
- }
- }
- sub gettags {
- my (%t, @mflac);
- foreach (@mflac) {
- if ($t{$tagname}) { next; }
- elsif ($tag[1]) {
- else { $t{$tagname} = 'null'; }
- }
- }
- sub getdirs {
- }
- sub getfiles {
- my @files;
- my $fn = "$dn/$_";
- if (/.flac$/ && -f $fn) {
- }
- }
- }
- sub name {
- my %t = gettags($fn);
- my $tref = \%t;
- my $dest;
- checktags($fn, $tref);
- my %ct = ( albumartist => $t{albumartist}, album => $t{album},
- discnumber => $t{discnumber}, tracknumber => $t{tracknumber},
- title => $t{title} );
- $ct{$tag} =~ s/[[:punct:]]//g;
- }
- my @dest = ($ENV{HOME}, 'lame', $ct{albumartist},
- $ct{album});
- unless (-d $dest) {
- #system('mkdir', '-p', $dest) or warn "Can't run 'mkdir': $!";
- }
- $ct{tracknumber}, $ct{title});
- $newfn = $dest . $newfn;
- }
- sub decode {
- lock(%decoded);
- lock($total);
- $decoded{$fn} = 1;
- }
- sub checktags {
- my %t = %{$tref};
- foreach my $tag ('discnumber', 'albumartist', 'album',
- 'tracknumber', 'title') {
- if (! $t{$tag}) {
- say "${fn}: missing '$tag' tag.";
- exit;
- }
- }
- }
- say "Starting threads";
- my @threads;
- foreach (1 .. $cpu_count) {
- }
- sub lame {
- while (!$q->pending() && !$end) {
- yield();
- }
- my $tid = threads->tid();
- my @old_opts = @opts;
- LOOP: while (my $fn = $q->dequeue_nb()) {
- my $oldfn = $fn;
- my ($newfn, $tref) = name($fn);
- my %t = %{$tref};
- foreach my $tag ('artist', 'title', 'album', 'tracknumber',
- 'date') {
- if ($t{$tag}) {
- given($tag) {
- when ('artist') {
- }
- when ('title') {
- }
- when ('album') {
- }
- when ('tracknumber') {
- }
- when ('date') {
- $t{date} =~ /[0-9]{4}/;
- }
- }
- }
- }
- say "thread${tid} ${newfn}: encoding...";
- @opts = @old_opts;
- lock(%pcm);
- lock($total);
- }
- if (!$q->pending() && !$end) {
- yield();
- }
- }
- my @files = getfiles($dn);
- if (!@files) {
- next;
- }
- foreach my $fn (@files) {
- decode($fn);
- # If the RAM disk is full, put decoded files in the queue,
- # and then yield until the LAME threads have cleared some RAM.
- while ($total > $disk_size) {
- $q->enqueue($fn);
- lock(%decoded);
- }
- yield();
- }
- }
- $q->enqueue($fn);
- lock(%decoded);
- }
- }
- $end = 1;
- say "All threads joined";
lame2flac perl script without use of external shells
Posted by Anonymous on Tue 25th Sep 2012 20:47
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.