Find a specified audio pattern in a collection of audio files - audio

I have a collection of 1000 audio files. Those files are music files stored in MP3 format, some are 10 minutes long, some are 15 seconds long.
I also have one audio file that is 1 second long, also in MP3. I would like to find which files from my collection contain this 1-second audio file. This 1-second audio file can have a slightly worse audio quality than my collection, because it was recoded, recompressed, etc, so I would need some more intelligent comparison method than simple binary search through my collection of files.
What should I read about if I'd like to create a small program for searching through my collection of audio files for this 1-second audio pattern?
Edit: In other words: Each file in my collection of 1000 files is a song. This 1-second fragment I'm searching for is a fragment of some song from this collection, and I'd like to know what song it's taken from.

Here's one way to do it using len(audioSegment) feaure of pydub.
"../so/*.mp3" holds your audio files. Or you can iterate through various folders using glob.
from glob import glob
from pydub import AudioSegment
for mp3_file in glob("../so/*.mp3"):
tmpAudioSegment = AudioSegment.from_mp3(mp3_file)
#Note: pydub len returns in milliseconds.
print("{} : {}".format(mp3_file,len(tmpAudioSegment)/1000))
Result
../so/Bubbles.mp3 : 10 sec
../so/Drone Dark Suspense 2.mp3 : 30 sec
../so/Sci-Fi Sweep.mp3 : 8 sec
../so/Pinball Machine.mp3 : 22 sec
../so/Title Flange Sweep Hit.mp3 : 3 sec
../so/Whale Sounds.mp3 : 6 sec
../so/Ambient Hit.mp3 : 2 sec
../so/Golf Hit 3.mp3 : 1 sec

Related

Best way to concatenate videos that have different resolution (generally 1080 or 720)

from command line or python would be best. and i am trying to concatenate around 15 clips of 45 seconds. preferably easy to automate with different number of videos and of different length.
Using MoviePy, which can import videos at a specific resolution:
from moviepy.editor import VideoFileClip, concatenate_videoclips
filenames = ["vid1.mp4", "vid2.mp4"] # You could generate this from os.listdir or similar
clips = []
for file in filenames:
clips.append(VideoFileClip(file, target_resolution=(1080, 1920))
final_clip = concatenate_videoclips(clips) # method="chain" by default. This is fine because all clips are the same size
final_clip.write_videofile("out.mp4")

How is the best way to play an MP3 file with a start time and end time in Python 3

I am working on a project that requires sound snippets to be played from MP3 files in a playlist. The files are full songs.
I have tried pygame mixer and I can pass the start time of the file, but I cannot pass the end time that I want the music to stop, or be able to fade-in and fade out the current snippet.
I have looked at the vlc and ffmpeg libraries, but I do not see the functionality I am looking for.
I'm hoping someone may be aware of a library out there that may be able to do what I am trying to accomplish.
I finally figured out how to do exactly what I wanted to do!
In the spirit of helping others I am posting an answer to my own question.
My development environment:
Mac OS Mojave 10.14.6
Python 3.7.4
PyAudio 0.2.11
PyDub 0.23.1
Here it is in it's most rudimentary form:
import pyaudio
from pydub import AudioSegment
# Assign a mp3 source file to the PyDub Audiosegment
mp3 = AudioSegment.from_mp3("path_to_your_mp3_file")
# Specify starting and ending offsets from the beginning of the stream
# then apply a fadein and fadeout. All values are in millisecond (seconds * 1000).
mp3 = mp3[int(43000):int(58000)].fade_in(2000).fade_out(2000)
# In the above example the music will start 43 seconds into the track with a 2 second
# fade-in, and only play for 15 seconds with a 2 second fade-out. If you don't need
# these features, just comment out the line and the full mp3 will play.
# Assign the PyAudio player
player = pyaudio.PyAudio()
# Create the stream from the chosen mp3 file
stream = player.open(format = player.get_format_from_width(mp3.sample_width),
channels = mp3.channels,
rate = mp3.frame_rate,
output = True)
data = mp3.raw_data
while data:
stream.write(data)
data=0
stream.close()
player.terminate()
It isn't in the example above, but there is a way to process the stream and increase/decrease/mute the volume of the music.
One other thing that could be done is to set up a thread to pause the processing (writing) of the stream, which would emulate a pause button in a player.

Specific mediainfo command for audio duration for media stream having stereo or multiple audio streams

I am getting concatenated duration of audios present in the file from the following command:
mediainfo --Output="Audio;%Duration%" "filename"
This command will give me the number 130406130468 which is concatenated duration of 2 audios present in the file.
with the following command:
mediainfo --Inform="Audio;%Duration/String1%" "filename"
The duration comes in "xxmn yys zzmsxxmn yys zzn" format where mn, s, ms are minutes, seconds and milliseconds. For this output it again requires context level parsing and is prone to errors when parsed in C language.
Is there any specific command to get the audio duration separately for separate audio streams.
Just add a separator in your MediaInfo template.
For example:
mediainfo --Output="Audio;%Duration%\n" "filename"
(note the "\n" which is the character meaning "line return")
or if you want to keep a single line:
mediainfo --Output="Audio;%Duration%, " "filename"
It maybe safer to show also the ID corresponding to the duration, e.g.
mediainfo --Output="Audio;Duration for track having ID %ID% is %Duration/String1%\n" "filename"
Jérôme, developer of MediaInfo.

dynamic generate m3u8 list with EXT-X-BYTERANGE

I want to fetch hls video segments by time interval.
Suppose I have seg1.m4s / seg2.m4s ..., and each seg is 1 minutes long and size is 10000 bytes.
If I want to play from 1:30 ~ 3:30, it means the hls would start from middle of seg2 to middle of seg4.
This why I would need to dynamic generate m3u8 list and EXT-X-BYTERANGE.
However I encounter some problem.
Here is my m3u8 list now
#EXTM3U
#EXT-X-VERSION:7
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-ALLOW-CACHE:YES
#EXT-X-MAP:URI="init.mp4",
#EXTINF:30.0,
#EXT-X-BYTERANGE:5000#5000,
seg2.m4s
#EXTINF:60.0,
seg3.m4s
#EXTINF:30.0,
#EXT-X-BYTERANGE:5000#0,
seg4.m4s
#EXT-X-ENDLIST
It would play only seg2.m4s and stop.

How many .ts files should be there in .m3u8 file for Live streaming?

For Http Live streaming, initially I have added 3 .ts files in to .m3u8 file it starts playing. In what ways should I append the incoming .ts file into .m3u8 file?
Shall I keep appending?
Shall I replace the older files with new one? If so in what order like, new set of files?
The best method I've seen is where you decide on the amount of "history" you want, for example H = 20 files, and then publish on the last X files in the playlist (if each segment is 10 seconds, then 3 files is a good idea).
you start by publishing movie_000, movie_001, and movie_002.
after 10 seconds you publish movie_001, and movie_002 and movie_003
...
and so on until you reach the amount of files you wish to have and then you rewrite older files (this way your hard drive doesn't overflow with data)
so after H files X 10 seconds, you will have movie_018, movie_019, movie_000

Resources