简体   繁体   English

使用 Moviepy Audiofile 将 mp4 中的音频保存为 wav 文件

[英]Saving audio from mp4 as wav file using Moviepy Audiofile

I have a video file named 'video.mp4' .我有一个名为'video.mp4'的视频文件。 I am trying to seperate a section of audio from the video and save it as a wav file that can be used with other Python modules.我试图从视频中分离出一段音频并将其保存为可与其他 Python 模块一起使用的 wav 文件。 I want to do this with MoviePy.我想用 MoviePy 做到这一点。

I send parameters to the write_audiofile function, specifying the filename, fps, nbyte, and codec.我向write_audiofile函数发送参数,指定文件名、fps、nbyte 和编解码器。

Following the MoviePy AudioClip docs , I specified the codec as 'pcm_s32le' for a 32-bit wav file.按照 MoviePy AudioClip docs ,我将 32 位 wav 文件的编解码器指定为'pcm_s32le'

from moviepy.editor import *

sound = AudioFileClip("video.mp4")
newsound = sound.subclip("00:00:13","00:00:15")   #audio from 13 to 15 seconds
newsound.write_audiofile("sound.wav", 44100, 2, 2000,"pcm_s32le")

This code generates a .wav file, named 'sound.wav' .此代码生成一个名为'sound.wav'.wav文件。


Opening the audio file in AudacityAudacity 中打开音频文件

The resulting file, sound.wav , can be opened in Audacity, however I run into problems when I try to use it as a wav file with other Python modules.生成的文件sound.wav可以在 Audacity 中打开,但是当我尝试将它用作带有其他 Python 模块的 wav 文件时遇到了问题。


Playing the sound file in pygamepygame中播放声音文件

import pygame
pygame.mixer.init()
sound=pygame.mixer.Sound("sound.wav")

The third line gives the following error:第三行给出了以下错误:

pygame.error: Unable to open file 'sound.wav'


Determining type of sound file using sndhdr.what()使用 sndhdr.what() 确定声音文件的类型

import sndhdr
sndhdr.what("sound.wav")

The sndhdr method returned none . sndhdr 方法返回none According to the docs , when this happens, the method failed to determine the type of sound data stored in the file.根据文档,发生这种情况时,该方法无法确定文件中存储的声音数据的类型。


Reading the file with Google Speech Recognition使用 Google 语音识别读取文件

import speech_recognition as sr
r = sr.Recognizer()
audio = "sound.wav"

with sr.AudioFile(audio) as source:
    audio = r.record(source)
text= r.recognize_google(audio)
print(text)

This code stops execution on the second to last line:此代码在倒数第二行停止执行:

ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format

Why does the audio file open in Audacity, if sndhdr.what() can not recognize it as an audio file type?为什么音频文件在 Audacity 中打开,如果sndhdr.what()无法将其识别为音频文件类型? How can I properly export a MoviePy AudioClip as a wav file?如何将 MoviePy AudioClip 正确导出为wav文件?

I had the same issue with no codec specified or with codec = 'pcms32le', the one that worked for me was pcm_s16le .我遇到了同样的问题,没有指定编解码器或编解码器 = 'pcm32le',对我有用的pcm_s16le Note that I am using "fr-FR" language, you should probably adapt to yur needs.请注意,我使用的是“fr-FR”语言,您可能应该适应您的需求。 here is the entire code :这是整个代码:

# Python code to convert video to audio
import moviepy.editor as mp
import speech_recognition as sr

# Insert Local Video File Path
clip = mp.VideoFileClip("/tmp/data/test.mp4")

# Insert Local Audio File Path
clip.audio.write_audiofile("/tmp/data/test.wav",codec='pcm_s16le')

# initialize the recognizer
r = sr.Recognizer()

# open the file
with sr.AudioFile("/tmp/data/test.wav") as source:
    # listen for the data (load audio to memory)
    audio_data = r.record(source)
    # recognize (convert from speech to text)
    text = r.recognize_google(audio_data, language = "fr-FR")
    print(text)

I had the same issue.我遇到过同样的问题。 I was trying to get a mp4 file from URL, then convert It into wav file and call Google Speech Recognition over It.我试图从 URL 获取 mp4 文件,然后将其转换为 wav 文件并通过它调用 Google Speech Recognition。 Instead I used pydub to handle conversion and it worked!相反,我使用 pydub 来处理转换并且它起作用了! Here's a sample of the code:下面是代码示例:

    import requests
    import io
    import speech_recognition as sr
    from pydub import AudioSegment


    # This function translate speech to text
    def speech_to_text(file):
        recognizer = sr.Recognizer()
        audio = sr.AudioFile(file)
        with audio as source:
            speech = recognizer.record(source)
            try:
                # Call recognizer with audio and language
                text = recognizer.recognize_google(speech, language='pt-BR')
                print("Você disse: " + text)
                return text
            # If recognizer don't understand
            except:
                print("Não entendi")

    def mp4_to_wav(file):
        audio = AudioSegment.from_file(file, format="mp4")
        audio.export("audio.wav", format="wav")
        return audio

    def mp4_to_wav_mem(file):
        audio = AudioSegment.from_file_using_temporary_files(file, 'mp4')
        file = io.BytesIO()
        file = audio.export(file, format="wav")
        file.seek(0)
        return file


    url = ''
    r = requests.get(url, stream=True)
    file = io.BytesIO(r.content)
    file = mp4_to_wav_mem(file)
    speech_to_text(file)

Note that I wrote two functions: mp4_to_wav and mp4_to_wav_mem.注意我写了两个函数:mp4_to_wav 和 mp4_to_wav_mem。 The only difference is mp4_to_wav_mem handle all files in memory and mp4_to_wav generates .wav file.唯一的区别是 mp4_to_wav_mem 处理内存中的所有文件,而 mp4_to_wav 生成 .wav 文件。

I read the docs of MoviePy and found that the parameter nbyte should be consistent with codec .我阅读了 MoviePy 的文档,发现参数nbyte应该与codec一致。 nbyte is for the Sample width (set to 2 for 16-bit sound, 4 for 32-bit sound). nbyte用于样本宽度(16 位声音设置为 2,32 位声音设置为 4)。 Hence, it better set nbyte=4 , when you set codec=pcm_s32le .因此,最好在设置codec=pcm_s32le时设置nbyte=4

i think this is the right method:我认为这是正确的方法:

import os
from moviepy.editor import AudioFileClip

PATH= "files/"
fileName = "nameOfYourFile.mp4"
newFileName = "nameOfTheNewFile"
Ext = "wav"
AudioFileClip(os.path.join(PATH, f"{fileName}")).write_audiofile(os.path.join(PATH, f"{newFileName}.{Ext}"))

I think this approach is very easy to understand.我认为这种方法很容易理解。

from moviepy.editor import *
input_file = "../Database/myvoice.mp4"
output_file = "../Database/myvoice.wav"
sound = AudioFileClip(input_file)
sound.write_audiofile(output_file, 44100, 2, 2000,"pcm_s32le")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM