Reputation: 3796
I wrote an app that uses ffmpeg to convert files to different fomat in a particular folder. The folder hosts .mpg, .mp3, .avi, .flv ... or whatever audio/video files one decides to add. The problem occurs when I use my app to select a file to convert that has spaces in the name; the app closes immediately, without having converted the file to a different format. Currently, I have to rename any files that has spaces in the name before it will convert via my app->ffmpeg. I'm attempting to write a function that will from the start of my app check file names in a particular folder for spaces, replace those spaces with underscores ('_'). I'm having some trouble with regex; I'm not good at writing/configuring them to solve problems. Can someone advise me on the correct regex to use to find white/blank space in a file name? Below is what I have so far:
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
I'm adding the section of code from my app that handles the call to ffmpeg, as requested:
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
Upvotes: 1
Views: 1540
Reputation: 103864
F.J's answer is great. If you are really concerned about spaces, and you do want to change them like iTunes does, I would only add that you may want to do something like this:
audio = ('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav')
spaces=['\t',' ','\r','\n','\v'] # etc -- other nasty space like characters...
if fName.endswith(audio) and any(space in fName for space in spaces):
return ''.join(c if c not in spaces else '_' for c in fName)
...
The you are catching all potential space like characters in fName into '_'
So if you have:
fName='file name\twith\nstuff\vin it'
it turns into:
file_name_with_stuff_in_it
Upvotes: 1
Reputation: 208475
Instead of regex, use use fName.endswith()
, you can provide a tuple as an argument of all of your audio extensions and it will return True
if fName
ends with any of them, for example:
audio = ('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav')
if fName.endswith(audio) and ' ' in fName:
return fName.replace(' ', '_')
return fName
If there are no spaces or it has a different extension, it will return the original string without changes.
Alternatively, you could use os.path.splitext()
:
audio = set(('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav'))
if os.path.splitext(fName)[1] in audio and ' ' in fName:
return fName.replace(' ', '_')
return fName
Upvotes: 6
Reputation: 12860
I don't think you need regex for this. You can simply use if ' ' in file:
Like so:
import os
dir = '.'
for file in os.listdir(dir)
if ' ' in file:
os.rename(file,file.replace(' ','_'))
Might want to print file
to test that you're in the right directory, before you try a mass rename.
If you need to make sure it's an acceptable file format, use os.path.splitext() to get the extension:
extensions = ('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav')
fname, ext = os.path.splitext(file)
if ext in extensions:
...
Upvotes: 1