Ejemplo n.º 1
0
    def convert_book(input_file, output_dir, ebookconvert_exe, extension):
        output_file_name, _ = os.path.splitext(os.path.basename(input_file))
        output_file = os.path.join(output_dir,
                                   output_file_name + '.' + extension)

        command = ebookconvert_exe
        command += ' "%s"' % sanitize_file_path_for_shell(input_file)
        command += ' "%s"' % sanitize_file_path_for_shell(output_file)

        print('Calling: %s' % command)

        exit_code = subprocess.call(command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)
Ejemplo n.º 2
0
    def transcode_file(input_file, output_dir, opusenc_exe, bitrate):
        output_file_name, _ = os.path.splitext(os.path.basename(input_file))
        output_file = os.path.join(output_dir, output_file_name + '.opus')

        # Transcoding flac to opus via opusenc.
        command = '%s --bitrate %s --discard-pictures ' % (opusenc_exe,
                                                           bitrate)
        command += '"%s" "%s"' % (sanitize_file_path_for_shell(input_file),
                                  sanitize_file_path_for_shell(output_file))

        print('Calling: %s' % command)

        exit_code = subprocess.call(command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)
Ejemplo n.º 3
0
    def transcode_file(input_file, output_dir, copy_tags, flac_exe, lame_exe,
                       metaflac_exe, quality):
        output_file_name, _ = os.path.splitext(os.path.basename(input_file))
        output_file = os.path.join(output_dir, output_file_name + '.mp3')

        # Piping flac's output into lame leads to a file with no tags at all.
        # Get all relevant tags from the source file beforehand if they should be copied.
        if copy_tags:
            tag_artist = normalize_tag(
                get_tag(metaflac_exe, input_file, 'ARTIST'))
            tag_title = normalize_tag(
                get_tag(metaflac_exe, input_file, 'TITLE'))
            tag_track_number = normalize_tag(
                get_tag(metaflac_exe, input_file, 'TRACKNUMBER'))
            tag_album = normalize_tag(
                get_tag(metaflac_exe, input_file, 'ALBUM'))
            tag_date = normalize_tag(get_tag(metaflac_exe, input_file, 'DATE'))
            tag_genre = normalize_tag(
                get_tag(metaflac_exe, input_file, 'GENRE'))
            tag_disk = normalize_tag(
                get_tag(metaflac_exe, input_file, 'DISCNUMBER'))
        else:
            tag_artist = ''
            tag_title = ''
            tag_track_number = ''
            tag_album = ''
            tag_date = ''
            tag_genre = ''
            tag_disk = ''

        # Transcode by piping flac' output into the lame encoder.
        command = '%s -c -d "%s" |' % (
            flac_exe, sanitize_file_path_for_shell(input_file))
        command += ' %s -%s' % (lame_exe, quality)
        if copy_tags:
            command += ' --add-id3v2 --pad-id3v2 --ignore-tag-errors'
            command += ' --ta "%s"' % sanitize_file_path_for_shell(tag_artist)
            command += ' --tt "%s"' % sanitize_file_path_for_shell(tag_title)
            command += ' --tn "%s"' % sanitize_file_path_for_shell(
                tag_track_number)
            command += ' --tl "%s"' % sanitize_file_path_for_shell(tag_album)
            command += ' --tg "%s"' % sanitize_file_path_for_shell(tag_genre)
            command += ' --ty "%s"' % sanitize_file_path_for_shell(tag_date)
            command += ' --tv "TPOS=%s"' % sanitize_file_path_for_shell(
                tag_disk)
        command += ' - "%s"' % sanitize_file_path_for_shell(output_file)

        print('Calling: %s' % command)

        exit_code = subprocess.call(command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)
Ejemplo n.º 4
0
    def transcode_file(input_file, output_dir, ffmpeg_exe, extension,
                       other_args):
        output_file_name, _ = os.path.splitext(os.path.basename(input_file))
        output_file = os.path.join(output_dir,
                                   output_file_name + '.' + extension)

        command = ffmpeg_exe
        command += ' -i "%s"' % sanitize_file_path_for_shell(input_file)
        if len(other_args) > 0:
            command += ' %s' % other_args
        command += ' "%s"' % sanitize_file_path_for_shell(output_file)

        print('Calling: %s' % command)

        exit_code = subprocess.call(command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)
Ejemplo n.º 5
0
    def upload_files(input_file, scp_exe, username, server_address, remote_path):
        command = scp_exe
        command += ' -r'
        command += ' -o StrictHostKeyChecking=no'
        command += ' "%s"' % sanitize_file_path_for_shell(input_file)
        command += ' %s@%s:%s' % (username, server_address, remote_path)

        print('Calling: %s' % command)

        exit_code = subprocess.call(command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)
def test_sanitize_file_path_for_shell():
    input_string = '$ABC "def" `ghi`'
    output_string = sanitize_file_path_for_shell(file_path=input_string)

    assert ('\\$' in input_string) is False
    assert ('\\$' in output_string) is True

    assert ('\\"' in input_string) is False
    assert ('\\"' in output_string) is True

    assert ('\\`' in input_string) is False
    assert ('\\`' in output_string) is True
Ejemplo n.º 7
0
    def reencode_file(input_file, output_dir, copy_tags, flac_exe,
                      metaflac_exe, compression):
        output_file_name, _ = os.path.splitext(os.path.basename(input_file))
        output_wav_file = os.path.join(output_dir, output_file_name + '.wav')
        output_flac_file = os.path.join(output_dir, output_file_name + '.flac')

        # Decoding flac to wav leads to the tags getting deleted.
        # Get all relevant tags from the source file beforehand if they should be copied.
        if copy_tags:
            tag_artist = normalize_tag(
                get_tag(metaflac_exe, input_file, 'ARTIST'))
            tag_title = normalize_tag(
                get_tag(metaflac_exe, input_file, 'TITLE'))
            tag_track_number = normalize_tag(
                get_tag(metaflac_exe, input_file, 'TRACKNUMBER'))
            tag_track_total = normalize_tag(
                get_tag(metaflac_exe, input_file, 'TRACKTOTAL'))
            tag_album = normalize_tag(
                get_tag(metaflac_exe, input_file, 'ALBUM'))
            tag_date = normalize_tag(get_tag(metaflac_exe, input_file, 'DATE'))
            tag_genre = normalize_tag(
                get_tag(metaflac_exe, input_file, 'GENRE'))
            tag_disk = normalize_tag(
                get_tag(metaflac_exe, input_file, 'DISCNUMBER'))
            tag_disk_total = normalize_tag(
                get_tag(metaflac_exe, input_file, 'DISCTOTAL'))
        else:
            tag_artist = ''
            tag_title = ''
            tag_track_number = ''
            tag_track_total = ''
            tag_album = ''
            tag_date = ''
            tag_genre = ''
            tag_disk = ''
            tag_disk_total = ''

        # Decode the flac file into a temporary wav file.
        decode_command = '%s -d "%s" -o "%s"' % (
            flac_exe, sanitize_file_path_for_shell(input_file),
            sanitize_file_path_for_shell(output_wav_file))

        print('Calling: %s' % decode_command)

        exit_code = subprocess.call(decode_command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)

        # Encode the wav file into flac, passing the tags.
        encode_command = '%s ' % flac_exe
        encode_command += '"%s" ' % sanitize_file_path_for_shell(
            output_wav_file)
        encode_command += '%s ' % compression
        encode_command += '-o "%s" ' % sanitize_file_path_for_shell(
            output_flac_file)
        if copy_tags:
            encode_command += '--tag=ARTIST="%s" ' % tag_artist
            encode_command += '--tag=TITLE="%s" ' % tag_title
            encode_command += '--tag=TRACKNUMBER="%s" ' % tag_track_number
            encode_command += '--tag=TRACKTOTAL="%s" ' % tag_track_total
            encode_command += '--tag=ALBUM="%s" ' % tag_album
            encode_command += '--tag=DATE="%s" ' % tag_date
            encode_command += '--tag=GENRE="%s" ' % tag_genre
            encode_command += '--tag=DISCNUMBER="%s" ' % tag_disk
            encode_command += '--tag=DISCTOTAL="%s" ' % tag_disk_total

        print('Calling: %s' % encode_command)

        exit_code = subprocess.call(encode_command, shell=True)
        if exit_code > 0:
            sys.exit(exit_code)

        # Delete the temporary wav file.
        os.remove(output_wav_file)