我编写了一个应用程序,它使用 ffmpeg 将文件转换为特定文件夹中的不同格式。该文件夹包含 .mpg、.mp3、.avi、.flv ... 或任何决定添加的音频/视频文件。当我使用我的应用程序选择名称中包含空格的要转换的文件时,就会出现问题;该应用程序会立即关闭,而无需将文件转换为其他格式。目前,我必须重命名名称中包含空格的所有文件,然后才能通过我的 app->ffmpeg 进行转换。我正在尝试编写一个函数,该函数将从我的应用程序开始检查特定文件夹中的文件名是否有空格,用下划线('_')替换这些空格。我在使用正则表达式时遇到了一些问题;我不擅长编写/配置它们来解决问题。有人可以建议我使用正确的正则表达式来查找文件名中的空白/空格吗?以下是我到目前为止的内容:
import os
import re
pattern = r"([^\s]+(?=\.(mp3|mov|mpg|mp4|flv|avi|mpeg4|mkv|mpeg|mpg2|.wav))\.\2)"
def replace_Wspace(self, fName):
if re.match(pattern, fName):
fname = fName.replace(' ', '_')
return fname
我正在根据要求从我的应用程序中添加处理对 ffmpeg 的调用的代码部分:
def convertButton(self, e):
unit1 = self.format_combo1.GetValue()
#Media Formats
unit2 = self.format_combo2.GetValue()
unit3 = self.format_combo3.GetValue()
unit4 = None
unit5 = self.format_combo5.GetValue()
bitRate = self.format_combo6.GetValue()
unit6 = bitRate
if unit3 == '-qmax':
unit4 = self.format_combo4.GetValue()
else:
pass
os.chdir("c:\\d-Converter\\ffmpeg\\bin")
wrkdir = os.getcwd()
newfile = unit1
stripped = newfile.strip('mpeg3aviovfl4w2c.') #Strips the extension from the original file name
progname='c:\\d-Converter\\ffmpeg\\bin\\ffmpeg.exe' + ' -i '
preset1_a='-vn -ar 44100 -ac 2 -ab'
preset1_b='-f mp3 '
preset_mp3='.mp3'
chck_unit1 = self.my_endswith(unit1)
while True:
if unit5 == 'video to mp3':
if unit6 == 'k/bs' or unit6 == '':
amsg = wx.MessageDialog(None, 'You must select a bit rate.', 'Media Converter', wx.ICON_INFORMATION)
amsg.ShowModal()
amsg.Destroy()
break
elif unit5 == 'video to mp3' and unit6 != 'k/bs' or unit6 != '':
self.button.Disable()
self.button2.Enable()
self.format_combo1.Disable()
self.format_combo2.Disable()
self.format_combo3.Disable()
self.format_combo4.Disable()
self.format_combo5.Disable()
self.format_combo6.Disable()
startWorker(self.LongTaskDone, self.LongTask3, wargs=(progname, wrkdir, unit1, preset1_a, unit6, preset1_b, stripped, preset_mp3))
break
elif unit1 != unit1.endswith(".mpg") or unit1.endswith(".mpeg") or unit1.endswith(".avi") or unit1.endswith(".mp4") or unit1.endswith(".flv"):
bmsg = wx.MessageDialog(None, 'You must select a valid format to convert to .mp3.', 'Media Converter', wx.ICON_INFORMATION)
bmsg.ShowModal()
bmsg.Destroy()
break
else:
pass
if unit1 == 'Select Media' or unit1 == '':
amsg = wx.MessageDialog(None, 'You must select a media file!', 'Media Converter', wx.ICON_INFORMATION)
amsg.ShowModal()
amsg.Destroy()
break
elif unit2 == 'Select Format' or unit2 == '' or unit2 == chck_unit1:
amsg = wx.MessageDialog(None, 'You must select a valid format', 'Media Converter', wx.ICON_INFORMATION)
amsg.ShowModal()
amsg.Destroy()
break
elif unit3 == 'Select Quality' or unit3 == '':
amsg = wx.MessageDialog(None, 'You must select quality', 'Media Converter', wx.ICON_INFORMATION)
amsg.ShowModal()
amsg.Destroy()
break
elif unit3 != 'Select Quality' or unit3 != '':
self.format_combo5.Disable()
if unit3 == '-qmax':
if unit4 == '0' or unit4 == '':
amsg = wx.MessageDialog(None, 'You must select number between 1-8.', 'Media Converter', wx.ICON_INFORMATION)
amsg.ShowModal()
amsg.Destroy()
break
else:
self.button.Disable()
self.button2.Enable()
self.format_combo1.Disable()
self.format_combo2.Disable()
self.format_combo3.Disable()
self.format_combo4.Disable()
self.format_combo5.Disable()
startWorker(self.LongTaskDone, self.LongTask2, wargs=(progname,wrkdir,unit1,unit3,unit4,stripped,unit2))
break
elif unit3 == '-sameq':
self.button.Disable()
self.button2.Enable()
self.format_combo1.Disable()
self.format_combo2.Disable()
self.format_combo3.Disable()
self.format_combo4.Disable()
self.format_combo5.Disable()
startWorker(self.LongTaskDone, self.LongTask, wargs=(progname,wrkdir,unit1,unit3,stripped,unit2))
break
def LongTask(self, progname, wrkdir, unit1, unit3, stripped, unit2):
convert_file1 = progname + wrkdir + '\\' + unit1 + ' ' + unit3 + ' ' + stripped + unit2
self.statusbar.SetStatusText("Converting: " + unit1 + "...")
os.system(convert_file1)
print convert_file1
def LongTask2(self, progname, wrkdir, unit1, unit3, unit4, stripped, unit2):
convert_file2 = progname + wrkdir + '\\' + unit1 + ' ' + unit3 + ' ' + unit4 + ' ' + stripped + unit2
self.statusbar.SetStatusText("Converting: " + unit1 + "...")
os.system(convert_file2)
def LongTask3(self, progname, wrkdir, unit1, preset1_a, unit6, preset1_b, stripped, preset_mp3):
convert_file3 = progname + wrkdir + '\\' + unit1 + ' ' + preset1_a + ' ' + unit6 + ' ' + preset1_b + stripped + preset_mp3
self.statusbar.SetStatusText("Converting: " + unit1 + "...")
os.system(convert_file3)
print convert_file3
def LongTask4(self, progdir, wrkdir, prog_dir, progdir3, f_string, s_string2, vid_format):
convert_file4 = progdir + f_string + prog_dir + s_string2 + progdir3 + f_string.strip('mpegaviw24ofl.') + vid_format
self.statusbar.SetStatusText("Converting: " + f_string + "...")
os.system(convert_file4)
print convert_file4