No audio on concatenating clips in MoviePy - python-3.x

I am trying to concatenate two clips using MoviePy [ Windows 10 , Python 3.7.4 ] , but there is no audio in the output video. I can see the temporary audio file while the videos are being concatenated.
from moviepy.editor import VideoFileClip, concatenate_videoclips
clip1 = VideoFileClip("C1.mp4")
clip2 = VideoFileClip("C2.mp4")
final_clip = concatenate_videoclips([clip1,clip2])
final_clip.write_videofile("my_concatenation.mp4")
Terminal gives this ouput,
Moviepy - Building video my_concatenation.mp4.
MoviePy - Writing audio in %s
MoviePy - Done.
Moviepy - Writing video my_concatenation.mp4
Moviepy - Done !
Moviepy - video ready my_concatenation.mp4
I have also tried this answer but it doesn't solve the issue. Any ideas why this might be happening?

Update MoviePy to v1.0.2 or greater, or apply the changes from https://github.com/Zulko/moviepy/pull/968 to your installation.

The issue is caused by a ffmpeg parameter , just go to moviepy -> video -> io -> ffmpeg_wrtier.py. Then search for ['-i', '-', '-an']. Then change the order into ['-an','-i','-']. Now the audio will work in any player. The first order binds the -an flag to the next stream, which is the audio file (which is subsequently ignored).

Related

How to play sound with python and no while loop

I want to play sound through my python script:
I have used win_sound
pyglet
playsound
and pygame but it doesn't want to work.
Is there any way to do this.
the directory is C:Users/Random/Folder/OtherFolder/Sound1
You can use librosa for loading audio data and sounddevice for playing audio.
Like this:
import time
import librosa
import sounddevice as sd
def play_audio(audio_path, sampling_rate=44100):
audio, sampling_rate = librosa.load(audio_path, sr=sampling_rate)
duration = librosa.core.get_duration(audio, sr=sampling_rate)
# play the audio
sd.play(audio, sampling_rate)
# wait until the audio is done playing
time.sleep(duration)
play_audio('./test.mp3')
Moreover, if this function causes the rest of your program to freeze ( because of time.sleep ) you need to run the play_audio function on a separate thread like here.
Try using the playsound module.
You need to install it first (enter in command prompt):
pip install playsound
And then you can use it as shown:
from playsound import playsound
playsound("C:Users\Random\Folder\OtherFolder\Sound1.wav")
(Replace the ".wav" thing with whatever file extension you have it set to).

pygame.mixer.Sound().play() gives no error but actually does not play

I am trying to play a .wav file with pygame
import pygame
pygame.mixer.init()
s = pygame.mixer.Sound('absolute path to file')
s.play()
it gives no error, but it seems like it doesn't play anything.
This is the complete output
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
installed pygame via pip in a conda python 3.6 enviroment. mac book pro 2019 with macOS Mojave
Add a line at the end :
input ('Press enter to continue.')
and you will hear it. Your script is ending before it has a chance to play the sound.
As bashBediam said the problem was that the code was ending not giving time to the audio to play. i fixed that doing like this.
import pygame
pygame.mixer.init()
s = pygame.mixer.Sound(path)
channel = s.play()
while channel.get_busy():
pygame.time.wait(100)

How to play .avi file in Google Colab?

I have found only ways to play '.mp4' files. I even tried to download .avi file and it is showing file is corrupted. I even tried to change .avi to .mp4 using online tools but nothing worked fine.
Use the python moviepy package
from moviepy.editor import *
path="file.avi"
clip=VideoFileClip(path)
clip.ipython_display(width=280)
You need to convert avi to mp4 with ffmepg
!ffmpeg -i input.avi output.mp4
Then get the file content into data_url
from IPython.display import HTML
from base64 import b64encode
mp4 = open('output.mp4','rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
Then display it with HTML()
HTML("""
<video controls>
<source src="%s" type="video/mp4">
</video>
""" % data_url)
Here's a working example.

Can I run a sound file (like mp3) from a Python script (example.py)?

I made a timer in a Python script using an IDE (its working fine). When the timer ends, I want to open a sound file, like a .mp3 file, so that the user knows that the timer went off. I know I could use google for a timer, but knowing how to do this would help for later projects. Thanks!
First install pygame via pip. Then use:
from pygame import mixer # Load the required library
mixer.init()
mixer.music.load('e:/LOCAL/Betrayer/. Metalik Klinik1-Anak Sekolah.mp3')
mixer.music.play()
if you're on linux you can use pyglet! pure python:
import pyglet
music = pyglet.resource.media('music.mp3')
music.play()
pyglet.app.run()
if you're on windows its built in:
import winsound
winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
EDIT
testing with winsound seemed to return nothing substantial so i found a work around using "playsound":
pip3 install playsound
in code:
import playsound
playsound.playsound("C:\\path\\to\\audio\\file.ext")
tested this with mp3 and wav and both worked just fine.
Let me know if this helped!

the packages pydub.AudioSegment in python doesn't work on window7?

I am using python on the window7, python3 ver, jupyter(or pycharm both).
I installed the pydub package for some reasons,
that converting *.mp3 to *.wav file at the same time
and split to mp3 file to per 40sec files,
and I checked it installed successfully.
"Requirement already satisfied: pydub in c:\programdata\anaconda3\lib\site-packages (0.22.1)"
but when I typed like bellows,
from pydub import AudioSegment
song = AudioSegment.from_mp3('D:\a'+'.mp3' )
it caused the error like:
C:\ProgramData\Anaconda3\lib\site-packages\pydub\utils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
pydub ffmpeg
so I try to installed ffmpeg but it's impossible with this message:
Could not find a version that satisfies the requirement subprocess (from ffmpeg) (from versions: )
No matching distribution found for subprocess (from ffmpeg)
so I found another way to solve it in here:
import pydub
pydub.AudioSegment.converter = "C:\path\to\ffmpeg.exe"
but another error shows up like this:
C:\ProgramData\Anaconda3\lib\site-packages\pydub\utils.py:193: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
as like many guys stuck in the same error.
I read many solutions about these, but nothing to make it better.
Anybody help me with this problem with a code in detail.
I have w8.
If i use this, i have а error.
AudioSegment.converter = ffmpeg_catalog+'ffmpeg.exe'
AudioSegment.ffmpeg = ffmpeg_catalog+'ffmpeg.exe'
AudioSegment.ffprobe = ffmpeg_catalog+'ffprobe.exe'
But when i add "C:\ffmpeg\bin" to PATH and restart os, it works.
And you must to remove lines above.

Resources