How to extract MXF file data? - python

Is there any way to extract MXF (Material Exchange Format) file data using python?
All I want to do is get data like video duration, actual video stream and if possible the voice in mp3 or any audio format from an MXF file.

You can do those things using ffmpeg-python.
Example of how to extract video duration:
import ffmpeg
filename = 'sample_960x400_ocean_with_audio.mxf'
# Get duration
# Credit: https://github.com/kkroening/ffmpeg-python/issues/545#issuecomment-836792082
video_info = ffmpeg.probe(filename)
duration = float(video_info['format']['duration'])
print(f'Duration: {duration} seconds')
Example of how to convert the audio to MP3:
import ffmpeg
filename = 'sample_960x400_ocean_with_audio.mxf'
# Load file
in_file = ffmpeg.input(filename)
# Get audio track, convert to mp3
in_file.output('file.mp3').run()
Example of how to convert the audio and video to mp4:
import ffmpeg
filename = 'sample_960x400_ocean_with_audio.mxf'
# Load file
in_file = ffmpeg.input(filename)
# Get video, convert to mp4
in_file.output('file.mp4').run()
Note that to use ffmpeg-python, you must install both ffmpeg-python and ffmpeg. See the documentation for more.

Related

Why isn't silence placed in the output audio file with AudioSegment.silent?

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")```

How to convert a audio-segment file into bytes type?

I want to make a speech recognition from a wav.
To do that, I have a wav that I split into multiple chunks, export them, and then use the SpeechRecognition library.
from pydub import AudioSegment
import speech_recognition as sr
r = sr.Recognizer()
for i in range(5):
audio = AudioSegment.from_wav("some_wav.wav")
audio_chunk=audio[int(i*1000):int(i*3000)]
audio_chunk.export('test.wav', format='wav')
detection = sr.AudioFile('test.wav')
with detection as source:
audio = r.record(source)
word = r.recognize_google(audio, language = 'ro-RO')
The problem is that this is not very optimal. I want to get rid of the export wav part. I want to transform the audio_chunk into bytes and then use it in the speechRecognition.AudioFile() with in-memory bytes.
Is there a way to convert the audio-segment type into bytes?

Overlay wav files at different start times using Pydub

I have a series of wav files I would like to combine and export as a single wav using Pydub. I would like the audio from the original files to play back at different times in the exported file e.g. the audio in audio_1.wav starts at time=0 in the exported file while the audio in audio_2.wav starts at time=5 instead of both starting at time=0 as the overlay function has them. Is there any way to do this? Below is the code I currently have for importing, overlaying, and exporting the audio files.
from pydub import AudioSegment
audio_1 = AudioSegment.from_file("audio_1.wav",
format="wav")
audio_2 = AudioSegment.from_file("audio_2.wav",
format="wav")
overlay = vln_audio_1.overlay(vla_audio_2)
file_handle = overlay.export("output2.wav", format="wav")
I didn't test it but based on documentation it may need overlay(..., position=5000)
BTW:
you may also add silence at the beginning to move audio
silence_5_seconds = AudioSegment.silent(duration=5000)
audio_2 = silence_5_seconds + audio_2

Python get list of streams from video (audio/video)

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.

Split Audio File with python Librosa

After doing split in an audio file with Librosa, I want to know how to obtain the resultant fragments in mp3 filesSee audio image
Can you just open individual files like
fragment1 = open("x.mp3", "a")
fragment2 = open("y.mp3", "a")
and then write to each of those using what you have as variables?

Categories

Resources