ちょっと、ディレクトリ内のすべてのflvビデオファイルをmp4形式に変換するこのスクリプトがあります。forループ-pythonでos.chdirを使用してディレクトリに変更する際にエラーが発生しました
すべてのファイルが1つのディレクトリに置かれているだけで動作するようになっていますが、変更する必要があるため、そのディレクトリ内のフォルダに移動し、各フォルダ内のファイルを変換します。ここで
は私のコードは、私はPythonスクリプトに新しいです。これは、私が
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'
を受け付けておりますエラーです
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()
あるので、本当に任意のヘルプを問題をうまくするための知識を持っていません感謝されます。私がdirpath変数から最初の文字を取ることを推測しなければならない場合。
'dirpath'は文字列です。あなたはなぜそれ以上のiteratigですか? –