I'm trying to set audio clip on image clip in moviepy. I read this page, then I wrote code like this.
from moviepy.editor import *
clip = ImageClip('img/output_1.png')
audio_clip = AudioFileClip('audio/output_1.mp3')
clip = clip.set_duration(audio_clip.duration)
clip = clip.set_audio(audio_clip)
Audio and duration are returns value.
>>> clip.audio
<moviepy.audio.io.AudioFileClip.AudioFileClip object at 0x1241dbd30>
>>> clip.duration
4.18
However when I write as file.
clip.write_videofile('mov/sample.mp4',fps=4)
I can't hear the sound. Are there any mistake on my code? I don't know how should I change my code... If you have any idea please help me!
My environment is
MacOS 11.3.1
python 3.9.5
moviepy 1.0.3
I had run same code on google colab, I got same problem. The video has 4.18 seconds, however it's no sound...
I downloaded audio file from ondoku3 and youtube as mp3
I found QuickTime can't play sound. When I play VLC, I can hear the sound.
And also when I merge audio and video with ffmpeg. I got same problem. So I think this problem from ffmpge convert problem or QuickTime problem.
Related
I've got this Python program and would like to know how I can play a video with the original audio.
I've tried other things about this.
Like pyglet(just a quick sample, no video):
import pyglet
import ffmpeg
player = Player()
source = load("Video Recording.m4a")
player.queue(source)
And stuff on StackOverflow two.
But it's all not working.
Are any of these libraries outdated?
Or any solutions?
quick question.
I'm running pygame under linux just to play some audio files.
I've got some .wav files and I'm having problems playing them back at the right speed.
import pygame.mixer, sys, time
#plays too fast
pygame.mixer.init(44100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
#plays too slow
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
I've ggogle code searched some stuff but everybody seems to be fine calling the init function with its default parameters. Can others try running this script and seeing if they get the same behavior or not? Does anybody know how to speed it up? Or adjust the speed for each file?
Thanks.
I had some mp3 audio tracks playing back slowed down. I updated the mixer frequency to be based on the mp3 sample rate using mutagen like so:
import pygame, mutagen.mp3
song_file = "your_music.mp3"
mp3 = mutagen.mp3.MP3(song_file)
pygame.mixer.init(frequency=mp3.info.sample_rate)
pygame.mixer.music.load(song_file)
pygame.mixer.music.play()
And it fixed the problem.
To improve Chris H answer. Here is a example of how to use the wave library.
import wave
import pygame
file_path = '/path/to/sound.wav'
file_wav = wave.open(file_path)
frequency = file_wav.getframerate()
pygame.mixer.init(frequency=frequency)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
Remember that if you want to change frequency or any other parameter used in pygame.mixer.init you must call pygame.mixer.quit first. Pygame documentation
Open your audio file in a free audio tool like Audacity. It will tell you the sampling rate of your media. It will also allow you to convert to a different sampling rate so all your sounds can be the same.
I figured it out...
There is a wave module http://docs.python.org/library/wave.html and it can read the sample rate for wav files.
If you're using Ogg Vorbis (.ogg) encoding, the same problem of stuttering audio happens. You'll have to read the frequency of what you're trying to play before initializing the mixer object.
Here's how to play .ogg audio with appropriate frequency using pygame.
from pyogg import VorbisFile
from pygame import mixer
# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()
I am trying to create a simple video of one image with audio with MoviePy. Working with the same code, sometimes MoviePy randomly produces greyed out and glitched videos like this: (opened with VLC Media Player)
The audio works fine in the final video, and the image used is also completely fine.
I tried searching but couldn't find anything that worked. I also re-downloaded FFMPEG, thinking it would solve the problem, but to no advantage.
The code I am using is:
# assume aud_path, vid_path and out_path are set and imports have been done
aud_clip = AudioFileClip(aud_path)
vid_clip = ImageClip(vid_path)
vid_clip = vid_clip.set_audio(aud_clip).set_duration(aud_clip.duration)
vid_clip.write_videofile(out_path, temp_audiofile='temp-audio.m4a', remove_temp=True, codec="libx264", audio_codec="aac", fps=24)
Any help will be appreciated.
I figured it out! For anyone having a similar problem, the problem was in the vid_clip.write_videfile() method in the codec argument.
I changed the codec to "mpeg4" and it is working fine now.
My main purpose is to change the pitch of the my voice and then input it to a voice room/voice call say like a zoom call or a hangouts meeting without playing it back to me.
I found 2 questions on a similar topic:
1. Playing mp3 file through microphone with python
2. How to play MP3 files into the microphone input jQuery
But these ones do not answer the question appropriately.
Something similar to this: https://github.com/jremmons/pyfakewebcam/blob/master/pyfakewebcam/pyfakewebcam.py
but for microphones?
You can use sounddevice, just tested it on Windows 10
you can create a stream and read data while as it records, naturally you will have a latency proportional to the size of the chunk you read.
This example records 6.4 seconds of audio at 16kHz. After finished you call the method stop. Each chunk in this example has 1024 samples corresponding to 64 milliseconds.
micStream = sd.InputStream(samplerate=16000, blocksize=1024, channels=1, dtype='float32')
micStream.start();
CHUNK_SIZE = 1024
NCHUNKS = 100
wavData = np.zeros(CHUNK_SIZE*NCHUNKS)
for i in range(0, NCHUNKS):
chunkData, overflowed = micStream.read(CHUNK_SIZE);
wavData[i*chunk:(i+1)*chunk] = np.mean(chunkData, axis=1) # ensure to be mono
micStream.stop()
plt.plot(wavData)
Required Module: pyaudio
install it through pip:
pip install pyaudio
NOTE: I've faced issues while trying to install pyaudio module in python 3.8 version but have read a lot of articles that with python 3.5 you won't face any issues.
If you want to save the audio recorded through microphone you can follow https://gist.github.com/mabdrabo/8678538 (to have .mp3 extension modify the variable WAVE_OUTPUT_FILENAME in the code to WAVE_OUTPUT_FILENAME = "file.mp3")
You can also use the SpeechRecognition module. follow this link https://www.youtube.com/watch?v=x8xjj6cR9Nc where he has created a python speech assistance (like google assistance) in a 30min video from scratch and github link to the code in video description.
I am getting a peculiar error while using PyMedia to convert video files (.avi) to audio files (.wav).
I get no error message - it just says python.exe has stopped working.
My code is here. The two sample video clips are here and here.
It works for the sample video s30_di_4.avi but not for the sample video s30_di_5.avi and fails with the above problem.
Can anyone please point out my mistake?