amr格式音频转为mp3格式

最近项目用到读取amr格式的音频,但是h5不支持这种格式,网上有一些是通过插件的方式来读取,amrlib.js之类,但是不擅长前端,而且有一些插件是要求把文件转为base64编码格式,然后把这些base64码当做参数传入一个js函数中,用着比较麻烦。

所以还是在后端转格式吧。

用到一个jar包,jave.jar

演示代码也比较简单,传入需要转换的amr音频文件所在的路径,以及新生成的mp3文件所在的路径就可以了。

import it.sauronsoftware.jave.*;

import java.io.File;

/**
 * Created by xhzhang on 2018/10/23.
 */
public class demo {
    public static void main(String[] args) throws Exception {
        String sourcePath = "D:/MediaRoot/123.amr";
        String targetPath = "D:/MediaRoot/123.mp3";
        changeToMp3(sourcePath, targetPath);
    }

    public static void changeToMp3(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();
        Encoder encoder = new Encoder();

        audio.setCodec("libmp3lame");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);

        try {
            encoder.encode(source, target, attrs);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C语言中将AMR文件换为MP3格式,你需要使用第三方库来完成。以下是一些常用的库和方法: 1. 使用FFmpeg库:FFmpeg是一个免费的开源软件,可以用于处理音频和视频文件。你可以使用libavcodec库来将AMR文件换为MP3格式。首先需要安装FFmpeg库,然后使用以下代码来进行换: ``` #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/avutil.h> int main() { AVFormatContext *fmt_ctx = NULL; AVCodecContext *codec_ctx = NULL; AVCodec *codec = NULL; AVPacket pkt; AVFrame *frame = NULL; int ret, i; av_register_all(); avcodec_register_all(); // open input file if ((ret = avformat_open_input(&fmt_ctx, "input.amr", NULL, NULL)) < 0) { fprintf(stderr, "Cannot open input file\n"); return ret; } // find audio stream info if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { fprintf(stderr, "Cannot find stream information\n"); return ret; } // find audio decoder codec = avcodec_find_decoder(fmt_ctx->streams[0]->codecpar->codec_id); if (!codec) { fprintf(stderr, "Unsupported codec\n"); return -1; } // allocate codec context codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Cannot allocate codec context\n"); return -1; } // copy codec parameters if ((ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[0]->codecpar)) < 0) { fprintf(stderr, "Cannot copy codec parameters\n"); return ret; } // open codec if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) { fprintf(stderr, "Cannot open codec\n"); return ret; } // allocate frame frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Cannot allocate frame\n"); return -1; } // initialize packet av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; // open output file FILE *outfile = fopen("output.mp3", "wb"); if (!outfile) { fprintf(stderr, "Cannot open output file\n"); return -1; } // read frames from input file while (av_read_frame(fmt_ctx, &pkt) >= 0) { // decode frame ret = avcodec_send_packet(codec_ctx, &pkt); if (ret < 0) { fprintf(stderr, "Error sending packet for decoding\n"); return ret; } while (ret >= 0) { ret = avcodec_receive_frame(codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error during decoding\n"); return ret; } // encode frame AVCodecContext *enc_ctx; AVCodec *enc; AVPacket enc_pkt; av_init_packet(&enc_pkt); enc_pkt.data = NULL; enc_pkt.size = 0; enc = avcodec_find_encoder(AV_CODEC_ID_MP3); if (!enc) { fprintf(stderr, "Codec not found\n"); return -1; } enc_ctx = avcodec_alloc_context3(enc); if (!enc_ctx) { fprintf(stderr, "Cannot allocate encoder context\n"); return -1; } enc_ctx->bit_rate = codec_ctx->bit_rate; enc_ctx->sample_rate = codec_ctx->sample_rate; enc_ctx->channels = codec_ctx->channels; enc_ctx->channel_layout = codec_ctx->channel_layout; enc_ctx->sample_fmt = enc->sample_fmts[0]; enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; if ((ret = avcodec_open2(enc_ctx, enc, NULL)) < 0) { fprintf(stderr, "Cannot open encoder\n"); return ret; } ret = avcodec_send_frame(enc_ctx, frame); if (ret < 0) { fprintf(stderr, "Error sending frame for encoding\n"); return ret; } while (ret >= 0) { ret = avcodec_receive_packet(enc_ctx, &enc_pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error during encoding\n"); return ret; } // write packet to output file fwrite(enc_pkt.data, 1, enc_pkt.size, outfile); av_packet_unref(&enc_pkt); } avcodec_free_context(&enc_ctx); } av_packet_unref(&pkt); } // close input file avformat_close_input(&fmt_ctx); // close output file fclose(outfile); // free resources avcodec_free_context(&codec_ctx); av_frame_free(&frame); return 0; } ``` 2. 使用其他第三方库:除了FFmpeg库,还有其他一些库可以用于音频换,如lame、libmp3lame等。你可以根据自己的需求选择适合自己的库。 需要注意的是,使用第三方库需要了解其使用方法和相关知识,建议在使用前仔细阅读文档和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值