안녕하세요, 디렉토리에있는 모든 flv 비디오 파일을 mp4 형식으로 변환하는이 스크립트가 있습니다.for 루프 - 파이썬에서 os.chdir을 사용하여 디렉토리로 변경하는 중 오류가 발생했습니다.
모든 파일이 하나의 디렉토리에 배치되었지만 이제는 해당 파일을 수정해야 해당 디렉토리 내의 폴더로 이동하여 각 폴더 내의 파일을 변환합니다. 여기
이 내가 수신하고있는 오류가 내 코드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'
그래서 정말 그렇게 도움이 문제를 해결하기 위해 지식을 가지고하지 않은 내가 파이썬 스크립팅에 새로운 오전 감사하겠습니다. dirpath 변수에서 첫 번째 문자를 가져온다 고 추측해야만한다면.
'dirpath'는 문자열입니다. 왜 너는 그걸 왜 반복하고 있니? –