我正在重写一个bash脚本,它从.pencast文件中提取aac文件(实际上只是.zip)......
import os
import glob
import zipfile
app_path = os.path.dirname(os.path.realpath(__file__)) + os.sep
temp = app_path + 'AudioFiles'
for pencast in (glob.glob( app_path + '*.pencast')):
f = zipfile.ZipFile(pencast, 'r')
for number, audio in enumerate(f.namelist()):
if 'aac' in audio:
print(os.path.basename(pencast), number, audio)
返回(所以你可以看到文件的样子)
:!/ usr / local / bin / python3 pencast.py
Cancer1-1.pencast 29 userdata/Sessions/PRS-a6959094a/audio-0.aac
Cancer1-1.pencast 32 userdata/Sessions/PRS-a695732e5/audio-0.aac
Cancer1-2.pencast 30 userdata/Sessions/PRS-a696fa7ab/audio-0.aac
Cancer1-2.pencast 33 userdata/Sessions/PRS-a699046df/audio-0.aac
Cancer1-3.pencast 32 userdata/Sessions/PRS-a699046df/audio-0.aac
Cancer1-3.pencast 35 userdata/Sessions/PRS-a696fa7ab/audio-0.aac
如何解压缩每个文件,给它一个唯一的名称,即
Cancer1-1-1.aac
Cancer1-1-2.aac
Cancer1-2-1.aac
...并且只将音频文件移至'AudioFiles'文件夹?
答案 0 :(得分:1)
首先,使用os.path.join连接路径。
尝试这样的事情:
import os
import glob
import zipfile
def is_audio(file):
return 'acc' in file
app_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(app_path)
try:
os.mkdir('AudioFiles')
except OSError:
pass
for pencast in (glob.glob('*.pencast')):
f = zipfile.ZipFile(pencast, 'r')
for number, audio in enumerate(filter(is_audio, f.namelist())):
basename = os.path.basename(os.path.splitext(pencast)[0])
newfilename = '{}-{}.acc'.format(basename, number)
print(newfilename, number, audio)
f.extract(audio, os.path.join('AudioFiles', newfilename))
我没有尝试代码,它可能包含错误,但我想你会明白这一点。