The output of the file is unchanged from the source.
I expect the following to mute the audio for a length of one second at two seconds into the audio file.
Python version: 3.7
from pydub import AudioSegment
audio_file = "input_audio.mp3"
# Load audio file into pydub
audio = AudioSegment.from_mp3(audio_file)
# place one second of silence two seconds in to mute that portion
audio = audio.overlay(AudioSegment.silent(duration=1000, frame_rate=audio.frame_rate), position=2000)
# Save audio with word muted to new file
audio.export("output audio.mp3", format="mp3")```
I'd like to include a short silence duration at the end of an audio clip. I haven't found any specific functions in the Moviepy documentation, so I've resorted to creating a muted audio file of 500ms and concatenating it with the original audio file.
In some cases, this concatenation will introduce a noticeable glitch at the intersection, and I haven't figured out why. I also realized by importing the concatenated audiofile to Audacity that Moviepy actually creates two audio tracks when concatenating.
Do you know a better way to add silence to the end of the clip, or maybe the reason why this glitch appears sometimes (in my experience about 1 every 4 instances)?
Here's my code:
from moviepy.editor import *
temp_audio = "original audio dir"
silence = "silence audio dir"
audio1 = AudioFileClip(temp_audio) #original audio file
audio2 = AudioFileClip(silence) #silence audio file
final_audio = concatenate_audioclips([audio1,audio2])
final_audio.write_audiofile(output)
I am currently using Python 3.9.5 and Moviepy 1.0.3
may be fps=44100
will work . mp3 file's frequence
You can use the below solution for adding the silence at the end or start of audio:
from pydub import AudioSegment
orig_seg = AudioSegment.from_file('audio.wav')
silence_seg = AudioSegment.silent(duration=1000) # 1000 for 1 sec, 2000 for 2 secs
# for adding silence at the end of audio
combined_audio = orig_seg + silence_seg
# for adding silence at the start of audio
#combined_audio = silence_seg + orig_seg
combined_audio.export('new_audio.wav', format='wav')
I have a video file and I want to get the list of streams from it. I can see the needed result by for example executing a simple `ffprobe video.mp4:
....
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661) ......
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), ......
....
But I need to use python and code that will work both on Windows and Ubuntu, without executing an external process.
My real goal is to check whether there is ANY audio stream within the video (a simple yes/no would suffice), but I think getting extra information can be helpful for my problem, so I'm asking about the entire streams
EDIT: Clarifying that I need to avoid executing some external process, but looking for some python code/library to do it within the process.
import os
import json
import subprocess
file_path = os.listdir("path to your videos folder")
audio_flag = False
for file in file_path:
ffprobe_cmd = "ffprobe -hide_banner -show_streams -print_format json "+file
process = subprocess.Popen(ffprobe_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
output = json.loads(process.communicate()[0])
for stream in output["streams"]:
if(stream['codec_type'] == 'audio'):
audio_flag = True
break;
if(audio_flag):
print("audio present in the file")
else:
print("audio not present in the file")
# loop through the output streams for more detailed output
for stream in output["streams"]:
for k,v in stream.items():
print(k, ":", v)
Note: Make sure that your videos folder path consist of only valid video files as i didn't include any file validation in the above code snippet. Also, I have tested this code for a video file that contains one video stream and one audio stream.
I am creating a program to turn text into speech (TTS).
What I've done so far is to split a given word into syllables and then play each pre-recorded syllables.
For example:
INPUT: [TELEVISION]
OUTPUT: [TEL - E - VI - SION]
And then the program plays each sound in order:
First: play TEL.wav
Second: play E.wav
Third: play VI.wav
Fourth: play SION.wav
I am using wave and PyAudio to play each wav file:
wf = wave.open("sounds/%s.wav" %(ss), 'rb')
p = pyaudio.PyAudio()
stream = p.open(...)
data = wf.readframes(CHUNK)
stream.write(data)
... etc.
Now the problem is that during the playback there is a delay between each audio file and the spoken word sounds unnatural.
Is it possible to mix these audio files without creating a new file and play them with 0.2s delay between each audio file?
Edit: I tried Nullman's solution and it worked better than just calling a new wf on each sound.
I also tried putting a crossfade following these instructions.
Related:
How to extract audio from a video file using python?
Extract audio from video as wav
How to rip the audio from a video?
My question is how could I extract wav audio track from video file, say video.avi?
I read many articles and everywhere people suggest to use (from Python) ffmpeg as a subprocess (because there are no reliable python bindings to ffmpeg - the only hope was PyFFmpeg but i found it is unmaintaned now). I don't know if it is right solution and i am looking for good one.
I looked to gstreamer and found it nice but unable to satisfy my needs -- the only way I found to accomplish this from command line looks like
gst-launch-0.10 playbin2 uri=file://`pwd`/ex.mp4 audio-sink='identity single-segment=true ! audioconvert ! audio/x-raw-int, endianness=(int)1234, signed=(boolean)true, width=(int)16, depth=(int)16, rate=(int)16000, channels=(int)1 ! wavenc ! filesink location=foo.wav’
But it is not efficient because i need to wait ages while playing video and simultaneously writing to wav file.
ffmpeg is much better:
avconv -i foo.mp4 -ab 160k -ac 1 -ar 16000 -vn ffaudio.wav
But i am unable to launch it from python (not as a command line subprocess). Could you please point me out pros and cons of launching ffmpeg from python as a command line utility ? (I mean using python multiprocessing module or something similar).
And second question.
What is simple way to cut long wav file into pieces so that i don't break any words ? i mean pieces of 10-20 sec length with start and end during the pause in sentences/words ?
i know how to break them on arbitrary pieces:
import wave
win= wave.open('ffaudio.wav', 'rb')
wout= wave.open('ffsegment.wav', 'wb')
t0, t1= 2418, 2421 # cut audio between 2413, 2422 seconds
s0, s1= int(t0*win.getframerate()), int(t1*win.getframerate())
win.readframes(s0) # discard
frames= win.readframes(s1-s0)
wout.setparams(win.getparams())
wout.writeframes(frames)
win.close()
wout.close()
It is a very easy Task using ffmpeg with python subprocess and there is a reason why people are pointing to this solution as a good solution.
This is the basic command extracting audio from a given video File:
ffmpeg -i test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav
The Python Code is just wrapping this command:
import subprocess
command = "ffmpeg -i C:/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav"
subprocess.call(command, shell=True)
You have to make sure that ffmpeg is a known task, so in your system environment variables, under path, the path to ffmpeg.exe should be listed, or you can just use the full path to the exe in your python code.
this could be better and easier to use than ffmpeg, it's called python-video converter, and can be used to extract the audio from video, https://github.com/senko/python-video-converter , it could be used in conjunction with mpg123, as follows
from converter import Converter
import os
c = Converter()
clip = 'clip.avi'
conv = c.convert(clip, 'audio.mp3', {'format':'mp3','audio':{'codec': 'mp3','bitrate':'22050','channels':1}})
for timecode in conv:
pass
os.system("mpg123 -w audio.wav audio.mp3")
the converter module extracts the audio from the video and saves it as an mp3 file, while mpg123 converts the mp3 file to mp4,
a different solution is as follows:
using moviepy module in python https://github.com/Zulko/moviepy
import moviepy.editor as mp
clip = mp.VideoFileClip("video.avi").subclip(0,20)
clip.audio.write_audiofile("theaudio.mp3")
the numbers within the subclip function specify start and end of audio, in seconds. you can then use mpg123 to change the audio to any other format
Audio clips can be created from an audio file or from the soundtrack of a video file
from moviepy.editor import *
audioclip = AudioFileClip("some_audiofile.mp3")
audioclip = AudioFileClip("some_video.avi")
https://zulko.github.io/moviepy/getting_started/audioclips.html
or example extract mp3 from
import os
VIDEOS_PATH = '/Users/****/videos'
VIDEOS_EXTENSION = '.webm' # for example
AUDIO_EXT = 'wav'
EXTRACT_VIDEO_COMMAND = ('ffmpeg -i "{from_video_path}" '
'-f {audio_ext} -ab 192000 '
'-vn "{to_audio_path}"')
os.chdir(VIDEOS_PATH)
files = os.listdir(VIDEOS_PATH)
for f in files:
if not f.endswith(VIDEOS_EXTENSION):
continue
audio_file_name = '{}.{}'.format(f, AUDIO_EXT)
command = EXTRACT_VIDEO_COMMAND.format(
from_video_path=f, audio_ext=AUDIO_EXT, to_audio_path=audio_file_name,
)
os.system(command)