Beispiel #1
0
def verify(progress, track):
    try:
        track.verify(progress)
        return (audiotools.Filename(track.filename).__unicode__(), track.NAME,
                None)
    except audiotools.InvalidFile as err:
        return (audiotools.Filename(track.filename).__unicode__(), track.NAME,
                str(err) if PY3 else unicode(err))
    except KeyboardInterrupt:
        return (audiotools.Filename(track.filename).__unicode__(), track.NAME,
                _.ERR_CANCELLED)
Beispiel #2
0
    def add_intro_outtro(self):
        '''
        Please add the intro and outro clips.
        http://audiotools.sourceforge.net/install.html
        prepend intro.flac
        append outro.flac
        '''

        output_filename = self.get_filename() + ".flac"
        if (os.path.exists(output_filename)):
            return

        output_filename = audiotools.Filename(output_filename)

        intro = audiotools.open_files(['intro.flac'])
        content = audiotools.open_files([self.input_file])
        outro = audiotools.open_files(['outro.flac'])

        audiofiles = [
            intro[0],
            content[0],
            outro[0],
        ]

        AudioType = audiotools.filename_to_type("intro.flac")
        metadata = content[0].get_metadata()
        output_class = AudioType
        #print audiofiles
        pcms = ([af.to_pcm() for af in audiofiles])
        #print [r.bits_per_sample for r in pcms]

        encoded = output_class.from_pcm(
            str(output_filename),
            audiotools.PCMReaderProgress(
                audiotools.PCMCat(pcms),
                sum([af.total_frames() for af in audiofiles]),
                progress_update),
            #output_quality,
            total_pcm_frames=sum([af.total_frames() for af in audiofiles]))
        encoded.set_metadata(metadata)

        self.set_intro_added()
Beispiel #3
0
def get_tracks(args, queued_files, accept_list=None):
    if accept_list is not None:
        accept_list = set(accept_list)

    for path in args:
        if os.path.isfile(path):
            try:
                filename = audiotools.Filename(path)
                if filename not in queued_files:
                    queued_files.add(filename)
                    track = open_file(str(filename))
                    if (accept_list is None) or (track.NAME in accept_list):
                        yield track
            except (audiotools.UnsupportedFile, IOError, OSError):
                continue
        elif os.path.isdir(path):
            for (d, ds, fs) in os.walk(path):
                for track in get_tracks([os.path.join(d, f) for f in fs],
                                        queued_files,
                                        accept_list=accept_list):
                    yield track
Beispiel #4
0
def accuraterip_checksum(progress, track, previous_track, next_track,
                         ar_matches):
    from audiotools import (transfer_data, PCMCat, PCMReaderHead,
                            PCMReaderDeHead, PCMReaderProgress)
    from audiotools.decoders import SameSample
    from audiotools.accuraterip import Checksum, match_offset

    # unify previous track, current track and next track into a single stream

    pcmreaders = []
    if previous_track is not None:
        frames_to_skip = previous_track.total_frames() - PREVIOUS_TRACK_FRAMES
        prev_pcmreader = previous_track.to_pcm()
        if hasattr(prev_pcmreader, "seek") and callable(prev_pcmreader.seek):
            frames_to_skip -= prev_pcmreader.seek(frames_to_skip)
        pcmreaders.append(PCMReaderDeHead(prev_pcmreader, frames_to_skip))
    else:
        pcmreaders.append(
            SameSample(sample=0,
                       total_pcm_frames=PREVIOUS_TRACK_FRAMES,
                       sample_rate=track.sample_rate(),
                       channels=track.channels(),
                       channel_mask=int(track.channel_mask()),
                       bits_per_sample=track.bits_per_sample()))

    pcmreaders.append(track.to_pcm())

    if next_track is not None:
        pcmreaders.append(PCMReaderHead(next_track.to_pcm(),
                                        NEXT_TRACK_FRAMES))
    else:
        pcmreaders.append(
            SameSample(sample=0,
                       total_pcm_frames=NEXT_TRACK_FRAMES,
                       sample_rate=track.sample_rate(),
                       channels=track.channels(),
                       channel_mask=int(track.channel_mask()),
                       bits_per_sample=track.bits_per_sample()))

    # feed stream to checksummer
    checksummer = Checksum(total_pcm_frames=track.total_frames(),
                           sample_rate=track.sample_rate(),
                           is_first=(previous_track is None),
                           is_last=(next_track is None),
                           pcm_frame_range=PREVIOUS_TRACK_FRAMES + 1 +
                           NEXT_TRACK_FRAMES,
                           accurateripv2_offset=PREVIOUS_TRACK_FRAMES)

    try:
        pcmreader = PCMReaderProgress(
            PCMCat(pcmreaders),
            PREVIOUS_TRACK_FRAMES + track.total_frames() + NEXT_TRACK_FRAMES,
            progress)
        audiotools.transfer_data(pcmreader.read, checksummer.update)
    except (IOError, ValueError) as err:
        return {
            "filename": audiotools.Filename(track.filename).__unicode__(),
            "error": str(err),
            "v1": {
                "checksum": None,
                "offset": None,
                "confidence": None
            },
            "v2": {
                "checksum": None,
                "offset": None,
                "confidence": None
            }
        }

    # determine checksum, confidence and offset from
    # the calculated checksums and possible AccurateRip matches
    (checksum_v2, confidence_v2,
     offset_v2) = match_offset(ar_matches,
                               checksums=[checksummer.checksum_v2()],
                               initial_offset=0)

    (checksum_v1, confidence_v1,
     offset_v1) = match_offset(ar_matches=ar_matches,
                               checksums=checksummer.checksums_v1(),
                               initial_offset=-PREVIOUS_TRACK_FRAMES)

    if len(ar_matches) == 0:
        return {
            "filename": audiotools.Filename(track.filename).__unicode__(),
            "error": None,
            "v1": {
                "checksum": checksum_v1,
                "offset": offset_v1,
                "confidence": AR_NOT_FOUND
            },
            "v2": {
                "checksum": checksum_v2,
                "offset": offset_v2,
                "confidence": AR_NOT_FOUND
            }
        }
    else:
        return {
            "filename": audiotools.Filename(track.filename).__unicode__(),
            "error": None,
            "v1": {
                "checksum":
                checksum_v1,
                "offset":
                offset_v1,
                "confidence": (confidence_v1 if
                               (confidence_v1 is not None) else AR_MISMATCH)
            },
            "v2": {
                "checksum":
                checksum_v2,
                "offset":
                offset_v2,
                "confidence": (confidence_v2 if
                               (confidence_v2 is not None) else AR_MISMATCH)
            }
        }
Beispiel #5
0
    parser.add_argument("filenames",
                        metavar="PATH",
                        nargs="+",
                        help=_.OPT_INPUT_FILENAME_OR_DIR)

    options = parser.parse_args()
    msg = audiotools.Messenger(options.verbosity == "quiet")

    if not options.accuraterip:
        queued_files = set()  # a set of Filename objects already encountered
        queue = audiotools.ExecProgressQueue(msg)
        for track in get_tracks(options.filenames, queued_files,
                                options.accept_list):
            queue.execute(
                function=verify,
                progress_text=audiotools.Filename(
                    track.filename).__unicode__(),
                completion_output=(display_results_tty if msg.output_isatty()
                                   else display_results),
                track=track)

        msg.ansi_clearline()
        try:
            results = queue.run(options.max_processes)
        except KeyboardInterrupt:
            msg.error(_.ERR_CANCELLED)
            sys.exit(1)

        formats = sorted(list({r[1] for r in results}))
        success_total = len([r for r in results if r[2] is None])
        failure_total = len(results) - success_total