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

时间:2019-04-06 14:33:57

标签: python wav moviepy

我有一个名为'video.mp4'的视频文件。我正在尝试从视频中分离出一部分音频,并将其另存为可用于其他Python模块的wav文件。我想用MoviePy做到这一点。

我将参数发送到write_audiofile函数,并指定文件名,fps,nbyte和编解码器。

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

此代码生成一个名为.wav的{​​{1}}文件。


Audacity中打开音频文件

生成的文件'sound.wav'可以在Audacity中打开,但是当我尝试将其与其他Python模块一起用作wav文件时遇到问题。


pygame中播放声音文件

sound.wav

第三行给出以下错误:

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


使用sndhdr.what()确定声音文件的类型

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

sndhdr方法返回了import sndhdr sndhdr.what("sound.wav") 。根据{{​​3}},当发生这种情况时,该方法无法确定文件中存储的声音数据的类型。


通过Google语音识别读取文件

none

此代码在倒数第二行停止执行:

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)

如果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 无法将其识别为音频文件类型,为什么音频文件以Audacity打开? 如何正确将MoviePy AudioClip导出为sndhdr.what()文件?

5 个答案:

答案 0 :(得分:0)

我有同样的问题。我试图从URL获取mp4文件,然后将其转换为wav文件,然后通过它调用Google语音识别。相反,我使用pydub来处理转换,并且有效!这是代码示例:

    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)

请注意,我编写了两个函数:mp4_to_wav和mp4_to_wav_mem。唯一的区别是mp4_to_wav_mem处理内存中的所有文件,而mp4_to_wav生成.wav文件。

答案 1 :(得分:0)

我阅读了MoviePy的文档,发现参数nbyte应该与codec一致。 nbyte用于样本宽度(对于16位声音,设置为2,对于32位声音,设置为4)。因此,最好在设置nbyte=4时设置codec=pcm_s32le

答案 2 :(得分:0)

我在未指定编解码器或编解码器='pcms32le'的情况下遇到了相同的问题,对我有用的是 pcm_s16le 。 请注意,我使用的是“ fr-FR”语言,您可能应该适应您的需求。 这是完整的代码:

# 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)

答案 3 :(得分:0)

我认为这是正确的方法:


Type=c("A", "B", 1, "C", 2, 3, 4)
Description=c("","","DNA1", "", "DNA2", "DNA3", "DNA4")
Gene=c("","","rp1", "", "rp2","rp3", "rp4")

data=data.frame(Type, Description, Gene, stringsAsFactors = FALSE)

答案 4 :(得分:0)

我认为这种方法很容易理解。

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