嘿,我已经有了这个脚本,它将目录中的所有flv视频文件转换为mp4格式。在for循环中使用os.chdir更改目录时出错 - python
我只是将所有文件放在一个目录中,但现在需要对其进行修改,以便它进入该目录中的文件夹并转换每个文件夹中的文件。
这里是我的代码
sourcedirectory="/home/dubit/Desktop/test/"
class ConvertToMP4:
def __init__(self):
self.flvfiles = []
def fetch_flv_files(self, directory_name):
print("Scanning directory: " + directory_name)
for (dirpath, dirnames, filenames) in os.walk(directory_name):
for files in filenames:
if files.endswith('.flv'):
print('convert file: ' + files)
self.flvfiles.append(files)
def convert_flv_file_to_mp4_file(self):
# check to see if the list is empty, if not proceed
num_of_dir = len(list(os.walk(sourcedirectory)))
if len(self.flvfiles) <= 0:
print("No files to convert!")
return
for x in range(num_of_dir):
for (dirpath, dirnames, filenames) in os.walk(sourcedirectory):
for z in dirpath:
os.chdir(z)
for flvfiles in filenames:
mp4_file = flvfiles.replace('.flv','.mp4')
cmd_string = 'ffmpeg -i "' + flvfiles + '" -vcodec libx264 -crf 23 -acodec aac -strict experimental "' + mp4_file + '"'
print('converting ' + flvfiles + ' to ' + mp4_file)
os.system(cmd_string)
def main():
usage = "usage: %prog -d <source directory for flv files>"
parser = OptionParser(usage=usage)
parser.add_option("-d","--sourcedirectory",action="store",
type="string", dest="sourcedirectory", default="./",
help="source directory where all the flv files are stored")
(options, args) = parser.parse_args()
flv_to_mp4 = ConvertToMP4()
flv_to_mp4.fetch_flv_files(sourcedirectory)
flv_to_mp4.convert_flv_file_to_mp4_file()
return 0
main()
这是我收到的错误
sudo ./sub_start_comp.py
Scanning directory: /home/dubit/Desktop/test/
convert file: 20051210-w50s.flv
convert file: barsandtone.flv
Traceback (most recent call last):
File "./sub_start_comp.py", line 66, in <module>
main()
File "./sub_start_comp.py", line 63, in main
flv_to_mp4.convert_flv_file_to_mp4_file()
File "./sub_start_comp.py", line 46, in convert_flv_file_to_mp4_file
os.chdir(z)
OSError: [Errno 2] No such file or directory: 'h'
我是新来的Python脚本等等都没有真正得到了知识,锻炼了问题,所以任何帮助将不胜感激。如果我不得不猜测它只是从dirpath变量中取出第一个字符。
'dirpath'是一个字符串。你为什么重复它? –