1

ここのbashハッカーは、私の足をpythonで濡らしています。私は自分の bash スクリプトの 1 つをほとんど Python に移植しましたが、python 変数を bash コマンド avconv に渡して .ogg オーディオ ファイルを .mp3 ファイルに変換する方法がわかりません (ため息mp3 プレーヤーは .ogg を再生しません)。

とにかく、持っているものを貼り付けようと思ったのですが、最後の「for」ループの「if」の最後の4行まで、すべて希望どおりに機能しています。もちろん、bash/avconv は python の 'for' ループから渡された python var 'f' を認識しません。

#!/usr/bin/python

#genre = GET SONG GENRE DIR
#if genre == rock+
#    sGenreDir = /media/multiMediaA_intHdA720Gb/music/heavierAlt

import os

def get_filepaths(directory):

    #This function will generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3 tuple (dirpath, dirnames, filenames).

    file_paths = []     # create list to store full paths to each song contained recursively within

    # walk the tree
    for root, directories, files in os.walk(directory):

        for filename in files:

            # join the two strings in order to know the fill file path
            filepath = os.path.join(root, filename)

            file_paths.append(filepath)     # add it to the list

    return file_paths

# run the avove function and store its results in a variable
#full_file_paths = get_filepaths("/media/multiMediaA_intHdA720Gb/music/heavierAlt")
full_file_paths = get_filepaths("/media/multiMediaA_intHdA720Gb/music/rockAndHeavier")

#print len(full_file_paths)

cntr = 0
for f in full_file_paths:

    if not (f.endswith(".mp3") or f.endswith(".ogg")):
        del full_file_paths[cntr]

    cntr = cntr + 1

#print len(full_file_paths)

# create random song list avoiding duplicate songs
destDir =  "/home/keithpc/tmp/rsList"    # will be selected by user
numSongs = 10   # number of songs will be chosen by the user
songList = []   # list to hold randomly selected songs
cntr = 1    # that will be incremented for each song appended to list 'songList'
while cntr <= numSongs:    #begin random song selection/list creation
    from random import choice
    newSong = choice(full_file_paths)

    if not newSong in songList:    # 'if' newSong is not yet list element then add it and ++cntr
        songList.append(newSong)
        cntr = cntr + 1

print len(songList)
for f in songList:
    print f

for f in songList:
    if f.endswith(".mp3"):    # cp .mp3 to player
        import shutil
        shutil.copy2(f, destDir)
    else:    # need to avconv .ogg before copying to player
        import os
        cmd = 'avconv -i f -c:a libmp3lame -q:a 4 -map_metadata 0:s $destDir/f/%ogg/mp3'
        os.system(cmd)

更新: src から dest にその場でコピーして「ogg」を「.mp3」に avconv しようとするランダムな曲の選択を扱う、上記のスクリプトの最後のセクションのみを貼り付けます

# begin to compile random song list
#destDir =  "/home/keithpc/tmp/rsList"   # will be var supplied by user ; employed in 'for' loop below instead
numSongs = 3   # number of songs var will be supplied by user
songList = []   # list to hold randomly selected songs
cntr = 1    # that will be incremented for each song appended to list 'songList'
while cntr <= numSongs:     # 'while' loop to iterate until 'numSongs' is matched by 'cntr'
    from random import choice
    newSong = choice(lFullFilePaths)    # randomly selected song from list 'lFullFilePaths' to add to list 'songList' if it is not a duplicate

    if not newSong in songList:     # 'if' var does not exist in list 'songList' then add it and cntr++
        songList.append(newSong)
        cntr = cntr + 1

#print len(songList)
#for f in songList:
#    print f

for f in songList:
    destDir =  "/home/keithpc/tmp/rsList"   # will be var supplied by user ; needs to reset each iteration or else it concantanates with previous song details
    if f.endswith(".mp3")   # 'if' song type is '.mp3' then copy direct to mp3 player
        import shutil
        shutil.copy2(f, destDir)
    else:   # 'else' song type must be '.ogg' and so must be avcon'd to mp3 on the fly to mp3 player
        import re   # regex function to extract the song name from its path
        sName = re.sub(r'^.*/', r'', f)     # extract song name from it's path
        #print sName
        import subprocess
        destDir = destDir + "/" + sName + "/%ogg/mp3"   # create single var 'destDir' containing args bash/avconv should need to convert '.ogg' sending its output as '.mp3' to mp3 player
        print(destDir)
        subprocess.call(['avconv', '-i', '%s' % f, '-c:a', 'libmp3lame', '-q:a', '4', '-map_metadata', '0:s', '%s' % destDir])

最後の「サブプロセス」コマンドがコメント化されている場合のターミナル出力は次のようになります。

07:54 python $ ./ranS*.py
3
/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Radiohead/The_Best_Of/02_Paranoid_Android.ogg
/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Rod_Stewart/If_We_Fall_In_Love_Tonight/
15_All_For_Love__With_Bryan_Adams_And_Sting.ogg
/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Smashing_PumpkinsThe/Siamese_Dream/12_sweet_sweet.ogg
02_Paranoid_Android.ogg
/home/keithpc/tmp/rsList/02_Paranoid_Android.ogg/%ogg/mp3
15_All_For_Love__With_Bryan_Adams_And_Sting.ogg
/home/keithpc/tmp/rsList/15_All_For_Love__With_Bryan_Adams_And_Sting.ogg/%ogg/mp3
12_sweet_sweet.ogg
/home/keithpc/tmp/rsList/12_sweet_sweet.ogg/%ogg/mp3

どちらが正しいように見えますか。

最終的な「サブプロセス」がコメント解除された端末出力は、次のようになります。

07:57 python $ ./ranS*.py
3
/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Guns_N_Roses/Patience_Live_At_The_Ritz_EP_Japanese/03_I_Used_To_Love_Her.ogg
/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/SmithsThe/The_Best_Of_The_Smiths_Vol_1/05_girlfriend_in_a_coma_the_best_of_the_smiths_vol._1.mp3
/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Oasis/The_Masterplan/14_The_Masterplan.ogg
03_I_Used_To_Love_Her.ogg
/home/keithpc/tmp/rsList/03_I_Used_To_Love_Her.ogg/%ogg/mp3
avconv version 0.8.9-6:0.8.9-1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  3 2013 02:10:51 with gcc 4.7.2
Input #0, ogg, from '/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Guns_N_Roses/Patience_Live_At_The_Ritz_EP_Japanese/03_I_Used_To_Love_Her.ogg':
  Duration: 00:02:48.69, start: 0.000000, bitrate: 186 kb/s
    Stream #0.0: Audio: vorbis, 44100 Hz, stereo, s16, 192 kb/s
    Metadata:
      ARTIST          : Guns N' Roses
      ALBUM           : Patience (Live At The Ritz) (EP - Japanese)
      TITLE           : I Used To Love Her
      DATE            : 1989
      GENRE           : Rock
      track           : 03
      CDDB            : 680adf09
Unable to find a suitable output format for '/home/keithpc/tmp/rsList/03_I_Used_To_Love_Her.ogg/%ogg/mp3'
14_The_Masterplan.ogg
/home/keithpc/tmp/rsList/14_The_Masterplan.ogg/%ogg/mp3
avconv version 0.8.9-6:0.8.9-1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  3 2013 02:10:51 with gcc 4.7.2
Input #0, ogg, from '/media/multiMediaA_intHdA720Gb/music/rockAndHeavier/Oasis/The_Masterplan/14_The_Masterplan.ogg':
  Duration: 00:05:22.80, start: 0.000000, bitrate: 197 kb/s
    Stream #0.0: Audio: vorbis, 44100 Hz, stereo, s16, 192 kb/s
    Metadata:
      ARTIST          : Oasis
      ALBUM           : The Masterplan
      TITLE           : The Masterplan
      DATE            : 1998
      GENRE           : Rock
      track           : 14
      CDDB            : c00fd90e
Unable to find a suitable output format for '/home/keithpc/tmp/rsList/14_The_Masterplan.ogg/%ogg/mp3'

おそらく問題は、avconv プロセスの一部として '.ogg' ext を '.mp3' ext に置き換えようとする /%ogg/mp3 にありますか? 誰かが私をホームフリーにするのを手伝ってくれる提案があれば、それは近いようです。

ところで、私は適切なヘルプを得るために、inet、特にstackoverflowを精査してきました。だから私は助けを求める前にRTFMを持っています。この最後の部分をbashシェルとやり取りすることができないようです。

ありがとう、昼寝

4

2 に答える 2

1

しばらく前に、大量のビデオを自分のゲーム コンソールで再生できる形式に変換する同様のスクリプトを作成しました。現在のコードで変更する内容は次のとおりです。

Martijn Pietersが推奨したように、os.system を使用して avconv を呼び出すのではなく、代わりにこれを試してください。

for f in songList:
    if f.endswith(".mp3"):    # cp .mp3 to player
        import shutil
        shutil.copy2(f, destDir)
    else:    # need to avconv .ogg before copying to player
        import subprocess
        subprocess.call(['avconv', '-i', '%s' % f, '-c:a',
                         'libmp3lame', '-q:a', '4', '-map_metadata',
                         '0:s', '$destDir/f/%ogg/mp3'])

変更点の簡単な説明は次のとおりです。まずos.systemsubprocess.call代わりにos. 次に、渡されるコマンドを straight ではなくavconvuseに再フォーマットしました。これにより、Python に「おい、この空白を埋めるために格納されているものを使用してください!」と伝えられますが、元のコードは「Python、ここで f を使用してください!」というものでした。渡されるコマンドを再フォーマットすると、各セグメントが引用符で囲まれていることに気付くでしょう。これは、サブプロセスが渡された引数を解釈する方法によるものです。他の誰かが理由と方法を私よりもはるかにうまく説明できると確信していますが、簡単に言えば、各引数の間に自動的にスペースが追加されます.'%s' % fff

- 編集 -

わかりました、更新された情報を確認した後、戻って提供されたコードを微調整しました. 私は家にいないため、これを完全にテストすることはできませんでした。現在、avconv を持っていないか、このコンピューターにインストールする機能がありません。shutilただし、 andsubprocess.call行を print ステートメントに置き換えたところ、すべてが「表示」されて動作するようになりました。

これがコードです。変更点の簡単な説明を以下に示します。

#!/usr/bin/python

import os
import subprocess
import shutil
from random import choice

def get_filepaths(directory):
    filenames = {}
    for root, directories, files in os.walk(directory):
        for file in files:
            filenames[file] = os.path.join(root, file)
    return filenames

def remove_file(d, key):
    r = dict(d)
    del r[key]
    return r

full_file_paths = get_filepaths("/media/multiMediaA_intHdA720Gb/music/rockAndHeavier")

to_remove = []

for file, file_path in full_file_paths.items():
    if not (file.endswith('.mp3') or file.endswith('.ogg')):
        to_remove.append(file)

for item in to_remove:
    full_file_paths = remove_file(full_file_paths, item)

destDir = "/home/keithpc/tmp/rsList"
songList = []
numSongs = 10
cntr = 1

while cntr <= numSongs:
    newSong = choice(full_file_paths.keys())
    if newSong not in songList:
        songList.append(newSong)
        cntr += 1

for f in songList:
    finalDest = destDir + '/%ogg/mp3'
    if f.endswith('.mp3'):
        shutil.copy2(full_file_paths.get(f), '%s/%s' %(destDir, f))
    else:
        subprocess.call(['avconv', '-i', '%s' % full_file_paths.get(f), '-c:a',
            'libmp3lame', '-q:a', '4', '-map_metadata', '0:s',
            '%s/%s.mp3' % (finalDest, f.strip('ogg'))])

最初の変更はget_filepaths定義で、最終的には名前の変更が少し簡単になったと思います。以前のようにリストを作成する代わりに、ファイルの実際のファイル名をキーとして使用して辞書を作成し、値はファイルへの完全なパスです。

次は

to_remove = []

def remove_file(d, key):
    r = dict(d)
    del r[key]
    return r

for file, file_path in full_file_paths.items():
    if not (file.endswith('.mp3') or file.endswith('.ogg')):
        to_remove.append(file)

for item in to_remove:
    full_file_paths = remove_file(full_file_paths, item)

これは、元は不要なファイルを削除していたが、代わりに辞書を処理するように書き直した方法に似ています。

最後の変更は、最終forループに対するものでした。この調整は主に、私が avconv (正式には FFmpeg として知られている) に関する重要なことを忘れたためです。つまり、avconv では、宛先ファイル名を指定する必要があります。私の記憶が正しければ、shutil は宛先ファイル名も知る必要がありますが、少なくとも Windows では正しく動作しませんでした。

for f in songList:
    finalDest = destDir + '/%ogg/mp3'
    if f.endswith('.mp3'):
        shutil.copy2(full_file_paths.get(f), '%s/%s' %(destDir, f))
    else:
        subprocess.call(['avconv', '-i', '%s' % full_file_paths.get(f), '-c:a',
            'libmp3lame', '-q:a', '4', '-map_metadata', '0:s',
            '%s/%s.mp3' % (finalDest, f.strip('.ogg'))])

お役に立てれば。

于 2013-12-30T13:41:58.090 に答える