当前位置:   article > 正文

Java实现视频,音频转码_java video audio encoder

java video audio encoder

需求:

  1. 把ape,ios,dsf,dff,ape,flac等音频格式转换为mp3,wav音频格式,因为大部分音乐播放器和html的< audio>标签都支持mp3和wav格式。

wav音乐格式是无损音乐,其他都是有损音乐格式或者无损音乐的压缩版。

  1. 把其他视频格式转换成HTML5能播放的视频格式,也可获取上传视频的时长,网页视频播放只支持一下三种:
格式解释
Ogg带有 Theora 视频编码和 Vorbis 音频编码的 Ogg 文件
MPEG4带有 H.264 视频编码和 AAC 音频编码的 MPEG 4 文件
WebM带有 VP8 视频编码和 Vorbis 音频编码的 WebM 文件

Jave介绍

JAVE(Java Audio Video Encoder),是一个包涵ffmpeg项目库。开发这可以运用它去实现音频(Audio)与视频(Video)文件的转码。
官方文档:http://www.sauronsoftware.it/projects/jave/manual.php

Jave中几个重要的类

1.Encoder类
	// 音频转换格式类
    Encoder encoder = new Encoder();
  • 1
  • 2

转码函数:

// 转码函数
public void encode(File source, 
					File target, 
					EncodingAttributes attributes,
 					EncoderProgressListener listener);
  • 1
  • 2
  • 3
  • 4
  • 5

参数说明:

  1. source:需要转码的源文件
  2. target : 需转型成的目标文件
  3. attributes:包含编码所需数据的参数
  4. listener :可选参数,实现该监听器可调用该接口中的方法,下面有说明
2.EncodingAttributes类
	// 设置转码属性
    EncodingAttributes attrs = new EncodingAttributes();
  • 1
  • 2

方法列表:

// 设置转码音频,添加音频转码时所需音频属性
public void setAudioAttributes(AudioAttributes audioAttributes)
// 设置转码视频,添加视频转码时所需音频属性
public void setVideoAttributes(VideoAttributes videoAttributes)
//设置转码格式
public void setFormat(String format)
//设置转码偏移位置,自定义转码开始时间
public void setOffset(Float offset)
//设置转码持续时间,自定义转码持续时间
public void setDuration(Float duration)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
3.AudioAttributes类:音频相关属性
	// 设置音频属性
    AudioAttributes audio = new AudioAttributes();
  • 1
  • 2

方法列表:

// 设置编码器
public void setCodec(String codec)
// 设置音频比特率
public void setBitRate(Integer bitRate)
// 设置音频节录率
public void setSamplingRate(Integer bitRate)
// 设置声音频道
public void setChannels(Integer channels)
// 设置音频音量
public void setVolume(Integer volume)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
4.VideoAttributes 类:视频相关属性
	// 设置视频属性
	VideoAttributes video = new VideoAttributes();
  • 1
  • 2

方法列表:

// 设置编码器
public void setCodec(String codec)
// 设置标签(通常用多媒体播放器所选择的视频解码)
public void setTag(String tag)
// 设置视频比特率
public void setBitRate(Integer bitRate)
// 设置视频帧率
public void setFrameRate(Integer bitRate)
// 设置视频大小
public void setSize(VideoSize size)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
5.MultimediaInfo类:媒体文件信息
    //创建媒体信息对象
    File source = new File("D:/test.mp4");
    MultimediaInfo info = encoder.getInfo(file);
  • 1
  • 2
  • 3

方法列表:

// 获得文件格式
public String getFormat();
// 获得时间(ms)
public long getDuration();
// 获得音频对象
public AudioInfo getAudio();
// 获得视频对象
public VideoInfo getVideo();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
6.EncoderProgressListener接口:监测转码操作
public interface EncoderProgressListener {
	//源文件信息
    void sourceInfo(MultimediaInfo var1);
	//增长千分率
    void progress(int var1);
	//转码信息提示
    void message(String var1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

实例

介绍:把除mp3和wav外的音乐格式转换成wav音乐格式

	/**
     * 转化音频格式
     * @param oldFormatPath : 原音乐路径
     * @param newFormatPath : 目标音乐路径
     * @return
     */
    public static boolean transforMusicFormat(String oldFormatPath, String newFormatPath) {
        File source = new File(oldFormatPath);
        File target = new File(newFormatPath);

        // 音频转换格式类
        Encoder encoder = new Encoder();

        // 设置音频属性
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec(null);
        // 设置转码属性
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("wav");
        attrs.setAudioAttributes(audio);

        try {
            encoder.encode(source, target, attrs);
            System.out.println("传唤已完成...");
            deleteFile(oldFormatPath);
            return true;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除原音频文件
     * @param filePath : 原文件路径
     * @return
     */
    public static boolean deleteFile(String filePath){
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            // 文件删除
            file.delete();
            return true;
        }
        return false;
    }

    /**
     * 判断格式是否需要转换
     * @param musicName
     * @return
     */
    public static boolean checkMusicFormat(String musicName){
        if (musicName== null || "".equals(musicName)){
            return false;
        }
        String suffix = musicName.substring(musicName.lastIndexOf(".")+1);
        if ("mp3".equals(suffix) || "wav".equals(suffix)){
            return false;
        }
        return true;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

总结

转换音频格式步骤:

  1. 创建转换格式类Encoder
  2. 设置音频属性,通过AudioAttributes类完成
  3. 设置转码属性,通过EncodingAttributes类完成
  4. 调用Encoder类的encode方法转码

视频格式转换类似。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/759594
推荐阅读
相关标签
  

闽ICP备14008679号