Can moviepy handle aiff audio? - python-3.x

I have a python script that pulls random audio clips from a hard drive. It finds wav and mp3 files and moviepy deals with them fine. When I added the aiff extensions to the search query I get this from moviepy...
codec = extensions_dict[ext[1:]]['codec'][0] KeyError: "'aiff'"
Does anyone know if moviepy specifically does not handle aiff audio?
Could there be a simple way to add aiff handling to moviepy?

Thanks for the head's up Tom. In case this can help someone else, here's what is working for me now.
for clip, title in audioClips.items(): #Iterate by key & value through the dictionary
#print(f"The title is {title}.")
if title.endswith('.aif') or title.endswith('aiff'):
clip.write_audiofile(dirpath+str(title),codec='pcm_s16le')
else:
clip.write_audiofile(dirpath+str(title))
clip.close()

Related

Adding watermark to video

I am able to use the moviepy library to add a watermark to a section of video. However when I do this it is taking the watermarked segment, and creating a new file with it. I am trying to figure out if it is possible to simply splice in the edited part back into the original video, as moviepy is EXTREMELY slow writing to the disk, so the smaller the segment the better.
I was thinking maybe using shutil?
video = mp.VideoFileClip("C:\\Users\\admin\\Desktop\\Test\\demovideo.mp4").subclip(10,20)
logo = (mp.ImageClip("C:\\Users\\admin\\Desktop\\Watermark\\watermarkpic.png")
.set_duration(20)
.resize(height=20) # if you need to resize...
.margin(right=8, bottom=8, opacity=0) # (optional) logo-border padding
.set_pos(("right","bottom")))
final = mp.CompositeVideoClip([video, logo])
final.write_videofile("C:\\Users\\admin\\Desktop\\output\\demovideo(watermarked).mp4", audio = True, progress_bar = False)
Is there a way to copy the 10 second watermarked snippet back into the original video file? Or is there another library that allows me to do this?
What is slow in your use case is the fact that Moviepy needs to decode and reencode each frame of the movie. If you want speed, I believe there are ways to ask FFMPEG to copy video segments without rencoding.
So you could use ffmpeg to cut the video into 3 subclips (before.mp4/fragment.mp4/after.mp4), only process fragment.mp4, then reconcatenate all clips together with ffmpeg.
The cutting into 3 clips using ffmpeg can be done from moviepy:
https://github.com/Zulko/moviepy/blob/master/moviepy/video/io/ffmpeg_tools.py#L27
However for concatenating everything together you may need to call ffmpeg directly.

batch FFMPEG-Normalize AND convert via Python?

I am currently working on a script to help me batch convert and
normalize audio files (wma to mp3)
In the search of useful tools I was lucky to stumble on FFMPEG-Normalize!
My script is running from Python and I am calling FFMPEG via subprocess.
I could not get the FFMPEG-Normalize to output Mp3 files - thus I am
doing another FFMPEG call to convert the resulted wav files.
Do you know how to make FFMPEG normalize also convert to mp3 ?
The second issue is that only part of the files in my folder are being
processed, I cant understand why. Out of 8 files I have in the path,
sometimes all of them are processed and sometimes only 3, or 5... very
weird!
Here is my code :
for file in sorted(os.listdir(pathdes)):
os.chdir(pathdes)
subprocess.call(['ffmpeg-normalize','-m','-l','-0.1',file])
file = 'normalized-' + file
file = file[:-3] + "wav"
file2 = file[:-3] + "mp3"
os.chdir(pathdes)
subprocess.call(['ffmpeg', '-i', file,'-b:a','320k', file2])
I understand FFMPEG normalize was written in Python, maybe there is
another way to call it other than subprocess ?
Am I missing something ? (i know i am !)
Thank you so much !
The ffmpeg-normalize tool allows you to set an audio encoder as well, using the -a, --acodec <acodec> option.
For example, to EBU R128-normalize a bunch of WAV files and encode them to MP3 with libmp3lame:
ffmpeg-normalize --ebu --acodec libmp3lame --extra-options "-b:a 192k" *.wav
Note that for MP3 specifically, you could use MP3Gain to change the volume without having to re-encode the files.

HandbrakeCLI command lines

I'm trying to convert DVD iso files to mp4 using HandbrakeCLI. I use the following line in a batch file:
D:\HandBrakeCLI.exe -i "D:\input.iso" -o "D:\output.mp4" --no-markers --width "720" --height "480" --preset "HQ 480p30 Surround" --encoder "mpeg2" --audio-lang-list "eng"
When I do this, I must then extract the audio from the file, using the following line:
D:\eac3to\eac3to.exe "D:\output.mp4" "D:\output.wavs" -down16
However, when I attempt to extract the audio, I get the error message
The format of the source file could not be detected.
Is there anything wrong with my former line of code that's causing the mp4 to get screwed up?
Minor side question: I'm also trying to get handbrake to remove subtitles and also only keep English audio, do you know what code could be used for that? I started a bit there with the --audio-lang-list "eng" but I'm now sure what to do from there.
Thanks a lot in advance!
You need to use a valid audio format. .wavs is not valid. You have to use an available audio codec to output to the below for --aencoder. The default output audio for MP4 is .aac
av_aac
copy:aac
ac3
copy:ac3
eac3
copy:eac3
copy:truehd
copy:dts
copy:dtshd
mp3
copy:mp3
vorbis
flac16
flac24
copy:flac
opus
copy
Defaults for audio
av_mp4 = av_aac
av_mkv = mp3
You need to pass none for no subtitles
-s none
And define only eng track like you were doing
--audio-lang-list eng
Check out the Handbrake CLI Documentation for the command line code:
https://handbrake.fr/docs/en/latest/cli/cli-guide.html
You can also try using a different program once you extract the audio. A program like XMediaRecode. It can also remux audio and video and convert other audio formats to wav
https://www.videohelp.com/software/XMedia-Recode

wx.Sound : wav file unsupported

I'm doing a game with wx.python and when I try to add a background music using wx.Sound I got the following error:
Python error:
Sound file 'images/game.wav' is in unsupported format.
This is the code I use. I've seen this code many times on the web but none had
the same problem:
sound = wx.Sound("images/game.wav")
sound.Play(wx.SOUND_ASYNC)
Sounds like you need to save the WAV file in a different format. WAV files are not all created equal.
http://en.wikipedia.org/wiki/WAV#WAV_file_compression_codecs_compared
Why not use wx.media or maybe the mplayer widget? I have a tutorial on each of these:
http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/
http://www.blog.pythonlibrary.org/2010/04/20/wxpython-creating-a-simple-mp3-player/

Combining multiple audio files in Python (with delay)

I'm looking to combine a range of different audio files (mp3) in Python. One of the requirements is that I need to be able to specify a delay at the end of each file. To illustrate, something like:
[file1.mp3--------3 seconds----------][delay---------2 seconds--------][file2.mp3]-------------4 seconds][delay---------2 seconds][file3.mp3----------3 seconds---------]
Does anyone here know of any mp3 libraries that can accomplish this? Python isn't really a necessity here. If it'll be easier in another language, that'll be fine.
I think FFmpeg can do this, given the right arguments. No real need to use a library.
To combine wav or aiff files, you can do something like this: (inspiration from here)
import aifc
def concatenate(*items):
data = []
for item in items:
f = aifc.open(item, 'rb')
data.append([f.getparams(), f.readframes(f.getnframes())])
f.close()
output = aifc.open('output.aif', 'wb')
output.setparams(data[0][0])
for item in data:
output.writeframes(item[1])
output.close()
See the link for the wav format (it's pretty much the same, but with the wave library)
To add silence, I would just make a one second silent file using your favorite audio editor and then concatenate in the proper amount of silence.

Resources