在这篇文章中,我们将带领您了解在Java中将mp3转换为WAV的全貌,包括javamp3转wav的相关情况。同时,我们还将为您介绍有关Nodejs怎么将MP3文件转换为WAV格式、Python使用Py
在这篇文章中,我们将带领您了解在Java中将mp3转换为WAV的全貌,包括java mp3转wav的相关情况。同时,我们还将为您介绍有关Nodejs怎么将MP3文件转换为WAV格式、Python使用Pydub将mp3转换为wav、Python使用静态图像将mp3转换为mp4、python将MP3转wave转成numpy的知识,以帮助您更好地理解这个主题。
本文目录一览:- 在Java中将mp3转换为WAV(java mp3转wav)
- Nodejs怎么将MP3文件转换为WAV格式
- Python使用Pydub将mp3转换为wav
- Python使用静态图像将mp3转换为mp4
- python将MP3转wave转成numpy
在Java中将mp3转换为WAV(java mp3转wav)
我将mp3转换为wav的代码是:
package audio1;import java.io.File;import javax.sound.sampled.AudioFileFormat;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;public class NewClass { public static void main(String [] args){ try{ AudioFileFormat inputFileFormat = AudioSystem.getAudioFileFormat(new File("c:\\1.mp3")); AudioInputStream ais = AudioSystem.getAudioInputStream(new File("c:\\1.mp3")); AudioFormat audioFormat = ais.getFormat(); System.out.println("File Format Type: "+inputFileFormat.getType()); System.out.println("File Format String: "+inputFileFormat.toString()); System.out.println("File lenght: "+inputFileFormat.getByteLength()); System.out.println("Frame length: "+inputFileFormat.getFrameLength()); System.out.println("Channels: "+audioFormat.getChannels()); System.out.println("Encoding: "+audioFormat.getEncoding()); System.out.println("Frame Rate: "+audioFormat.getFrameRate()); System.out.println("Frame Size: "+audioFormat.getFrameSize()); System.out.println("Sample Rate: "+audioFormat.getSampleRate()); System.out.println("Sample size (bits): "+audioFormat.getSampleSizeInBits()); System.out.println("Big endian: "+audioFormat.isBigEndian()); System.out.println("Audio Format String: "+audioFormat.toString()); AudioInputStream encodedASI = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais); try{ int i = AudioSystem.write(encodedASI, AudioFileFormat.Type.WAVE, new File("c:\\converted.wav")); System.out.println("Bytes Written: "+i); }catch(Exception e){ e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); } }}
但是我得到以下输出:
File Format Type: MP3File Format String: MP3 (.mp3) file, byte length: 9631340, data format: MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, , frame length: 10030File lenght: 9631340Frame length: 10030Channels: 2Encoding: MPEG1L3Frame Rate: 41.666668Frame Size: -1Sample Rate: 48000.0Sample size (bits): -1Big endian: trueAudio Format String: MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, java.lang.ArrayIndexOutOfBoundsException: 1 at org.tritonus.sampled.convert.javalayer.MpegFormatConversionProvider$DecodedMpegAudioInputStream$DMAISObuffer.append(MpegFormatConversionProvider.java:386) at javazoom.jl.decoder.Obuffer.appendSamples(Unknown Source) at javazoom.jl.decoder.SynthesisFilter.compute_pcm_samples(Unknown Source) at javazoom.jl.decoder.SynthesisFilter.calculate_pcm_samples(Unknown Source) at javazoom.jl.decoder.LayerIIIDecoder.decode(Unknown Source) at javazoom.jl.decoder.LayerIIIDecoder.decodeFrame(Unknown Source) at javazoom.jl.decoder.Decoder.decodeFrame(Unknown Source) at org.tritonus.sampled.convert.javalayer.MpegFormatConversionProvider$DecodedMpegAudioInputStream.execute(MpegFormatConversionProvider.java:307) at org.tritonus.share.TCircularBuffer.read(TCircularBuffer.java:138) at org.tritonus.share.sampled.convert.TAsynchronousFilteredAudioInputStream.read(TAsynchronousFilteredAudioInputStream.java:194) at javax.sound.sampled.AudioInputStream.read(AudioInputStream.java:292) at com.sun.media.sound.PCMtoPCMCodec$PCMtoPCMCodecStream.read(PCMtoPCMCodec.java:506) at com.sun.media.sound.SunFileWriter$NoCloseInputStream.read(SunFileWriter.java:199) at java.io.SequenceInputStream.read(SequenceInputStream.java:208) at java.io.SequenceInputStream.read(SequenceInputStream.java:211) at java.io.InputStream.read(InputStream.java:101) at com.sun.media.sound.WaveFileWriter.writeWaveFile(WaveFileWriter.java:247) at com.sun.media.sound.WaveFileWriter.write(WaveFileWriter.java:145) at javax.sound.sampled.AudioSystem.write(AudioSystem.java:1354) at audio1.NewClass.main(NewClass.java:33)
谁能帮我我在做什么错?
答案1
小编典典public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception{ if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){ throw new IllegalArgumentException("Illegal Argument passed to this method"); } ByteArrayInputStream bais = null; ByteArrayOutputStream baos = null; AudioInputStream sourceAIS = null; AudioInputStream convert1AIS = null; AudioInputStream convert2AIS = null; try{ bais = new ByteArrayInputStream(sourceBytes); sourceAIS = AudioSystem.getAudioInputStream(bais); AudioFormat sourceFormat = sourceAIS.getFormat(); AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false); convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS); convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS); baos = new ByteArrayOutputStream(); byte [] buffer = new byte[8192]; while(true){ int readCount = convert2AIS.read(buffer, 0, buffer.length); if(readCount == -1){ break; } baos.write(buffer, 0, readCount); } return baos.toByteArray(); } catch(UnsupportedAudioFileException uafe){ //uafe.printStackTrace(); throw uafe; } catch(IOException ioe){ //ioe.printStackTrace(); throw ioe; } catch(IllegalArgumentException iae){ //iae.printStackTrace(); throw iae; } catch (Exception e) { //e.printStackTrace(); throw e; }finally{ if(baos != null){ try{ baos.close(); }catch(Exception e){ } } if(convert2AIS != null){ try{ convert2AIS.close(); }catch(Exception e){ } } if(convert1AIS != null){ try{ convert1AIS.close(); }catch(Exception e){ } } if(sourceAIS != null){ try{ sourceAIS.close(); }catch(Exception e){ } } if(bais != null){ try{ bais.close(); }catch(Exception e){ } } } }
这里的sourceBytes表示MP3文件或WAV文件。audioFormat是您要转换的PCM格式。另外,我们需要将javazoom.com中的mp3spi.jar,tritonus_mp3.jar,jl* .jar,tritonus_share.jar放入类路径中。希望这对其他人有帮助。
Java 7版本:
public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception { if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){ throw new IllegalArgumentException("Illegal Argument passed to this method"); } try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes); final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) { AudioFormat sourceFormat = sourceAIS.getFormat(); AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false); try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS); final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte [] buffer = new byte[8192]; while(true){ int readCount = convert2AIS.read(buffer, 0, buffer.length); if(readCount == -1){ break; } baos.write(buffer, 0, readCount); } return baos.toByteArray(); } }}
Maven:
<dependency> <groupId>com.googlecode.soundlibs</groupId> <artifactId>mp3spi</artifactId> <version>1.9.5-1</version></dependency><dependency> <groupId>com.googlecode.soundlibs</groupId> <artifactId>jlayer</artifactId> <version>1.0.1-1</version> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions></dependency>
Nodejs怎么将MP3文件转换为WAV格式
随着时间的推移,音频格式的多样性已经变得非常丰富。mp3 和 wav 也成为了最常用的音频格式之一,然而,由于wav 在音质上比 mp3 清晰,近年来,越来越多的人开始考虑将 mp3 转换为 wav 格式。而 nodejs 也成为了一种流行的编程语言,它的出现已经使视频和音频的转换更加容易。在本文中,我们将介绍如何使用 nodejs 将 mp3 文件转换为 wav 格式。
一、环境设定
在开始使用 Nodejs 进行音频格式转换之前,您应该确保拥有一个可用的 Nodejs 环境和一些必要的模块。您可以从官方网站 https://nodejs.org/en/ 下载最新的 Nodejs。
在正确安装了 Nodejs 环境后,我们需要下载几个新的依赖项。打开命令行,并在本地文件夹中执行以下命令:
npm install fluent-ffmpeg
npm install ffmpeg
二、开始转换
在安装完所有必备的模块后,我们可以正式开始进行音频格式转换了。
首先,您需要将要转换的 MP3 文件放在本地文件夹中。然后,您可以在 Nodejs 中使用 fluent-ffmpeg 模块进行 MP3 转 WAV 文件的转换。
const ffmpeg = require(''fluent-ffmpeg''); const fs = require(''fs''); //请自行将[audio.mp3]替换成要转换的文件名 ffmpeg(''./audio.mp3'') .toFormat(''wav'') .on(''error'', function (err) { console.log(''An error occurred: '' + err.message); }) .on(''end'', function () { console.log(''Processing finished !''); }) .pipe(fs.createWriteStream(''./audio.wav''));
您可以将上述代码保存并命名为 audio.js,然后在命令行中执行以下命令:
node audio.js
启动脚本后,程序将开始运行,并将 MP3 文件转换为 WAV 文件。 转换后的文件将储存在本地文件夹中。
三、总结
如上所述,使用 Nodejs 将 MP3 文件转换为 WAV 文件可以说是一种相对简单的操作。您可以通过安装 Nodejs 和使用 fluent-ffmpeg 模块来实现这一目标。
因此,无论是在工作中,学习中,甚至是在个人生活中,只要您有需要将 MP3 文件转换为 WAV 文件,上述方法都将是十分有用。
以上就是Nodejs怎么将MP3文件转换为WAV格式的详细内容,更多请关注php中文网其它相关文章!
Python使用Pydub将mp3转换为wav
好的,现在我坚持将mp3转换为wav。我看到了不同的答案,但我想我会去使用pydub之一,我已经使用这几行代码做了
from pydub import AudioSegmentAudioSegment.from_mp3("/input/file.mp3").export("/output/file.wav", format="wav")
但是当我运行上面的代码时,出现以下错误
C:\ Python27 \ lib \ site-packages \ pydub-0.14.2-py2.7.egg \ pydub \
utils.py:165:运行时警告:找不到ffmpeg或avconv-默认为ffmpeg,但可能不起作用追溯(最近一次通话最后一次):文件stereo_to_mono()中的文件“
C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py”,第7行文件“ C:\ Users \ phourlhar \ Desktop \ VoiceDetector \
utils.py”,第25行,位于stereo_to_mono中sound = AudioSegment.from_mp3(PROJECT_DIR+''\\files\\rec''+str(c)+''.mp3'')
from_file中的第346行的文件“ build \ bdist.win32 \ egg \ pydub \ audio_segment.py”
init errread,errwrite中的文件“ C:\ Python27 \ lib \ subprocess.py”,第711行)
_execute_child startupinfo中的文件“ C:\ Python27 \ lib \ subprocess.py”,第948行)
WindowsError:[错误2]系统找不到指定的文件
我不知道为什么会引发此错误,因为我非常确定文件存在。虽然我有建议安装ffmpeg的答案,但我不知道以后是否会以任何方式影响应用程序的部署
答案1
小编典典该pydub
模块使用ffmpeg
或avconf
程序进行实际转换。因此,您必须进行安装ffmpeg
才能使其正常工作。
但是,如果您不需要pydub
其他任何东西,则可以使用内置subprocess
模块来调用转换器程序,ffmpeg
如下所示:
import subprocess subprocess.call([''ffmpeg'', ''-i'', ''/input/file.mp3'', ''/output/file.wav''])
顺便说一下,这要求ffmpeg二进制文件位于$ PATH中的某个位置。
编辑 :用ffmeg
,据我所知,您不能将立体声转换为单声道。您只能 选择 左或右声道。我假设这不是您想要的。
该sox
程序 可以 将立体声转换为单声道:
import subprocess subprocess.call([''sox'', ''/input/file.mp3'', ''-e'', ''mu-law'', ''-r'', ''16k'', ''/output/file.wav'', ''remix'', ''1,2''])
这将以16 kHz的速率进行采样,采样率为8位,从而为您提供16 kb / s。
Python使用静态图像将mp3转换为mp4
ffmpeg -loop 1 -framerate 1 -i image.png -i audio.mp3 -map 0:v -map 1:a -r 10 -vf "scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p" -movflags +faststart -shortest -fflags +shortest -max_interleave_delta 100M output.mp4
-
-loop 1
使input.png
无限循环。 -
-framerate 1
将input.png
输入帧频设置为1 fps。 -
-map 0 -map 1:a
从image.png
中选择视频,而从audio.mp3
中选择音频。如果image.png
小于MP3附带的任何专辑/封面,则需要这样做。否则,它可能会选择专辑/封面。有关更多信息,请参见FFmpeg Wiki: Map。 -
-r 10
将输出帧频设置为10 fps。将输入设置为1 fps,将输出设置为10 fps有两个原因:- 与最初将输入设置为10 fps相比,以1 fps的速度输入和复制帧至10 fps的速度更快。它使编码更快。
- 大多数播放器无法播放约6 fps以下的内容。 10是安全值。
-
scale='iw-mod(iw,2)'
使用scale filter确保输出宽度和高度均可以被2整除,这是某些编码器的要求。这使您可以使用任意大小的图像作为输入。否则,您会收到错误消息:width not divisible by 2
。 -
format=yuv420p
format filter使输出使用YUV 4:2:0色度子采样以实现播放兼容性。 -
-movflags +faststart
使视频开始播放更快。 -
-shortest
的输出与audio.mp3
一样长。这是必需的,因为使用了-loop 1
。 -
-fflags +shortest -max_interleave_delta 100M
与-shortest
相关,由于ffmpeg的怪异行为,在某些情况下需要使用。有关说明,请参见My ffmpeg output always add extra 30s of silence at the end。
某些库,例如ffmpy
和ffmpeg-python
可以使您使用python操作ffmpeg,请尝试一下。 (我很久以前就使用ffmpeg-python,效果很好)
python将MP3转wave转成numpy
import waveimport osimport numpy as npfrom pydub import AudioSegmentimport matplotlib.pylab as plt%matplotlib inline wav_file = 'Now.wav'song = AudioSegment.from_mp3('music.mp3')song.export(wav_file , format="wav")#Z转成numpywav =np.frombuffer(AudioSegment.from_mp3(wav_path).raw_data, dtype="int16")wav = wav.astype("float")
关于在Java中将mp3转换为WAV和java mp3转wav的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Nodejs怎么将MP3文件转换为WAV格式、Python使用Pydub将mp3转换为wav、Python使用静态图像将mp3转换为mp4、python将MP3转wave转成numpy的相关知识,请在本站寻找。
本文标签: