有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java将ogv视频文件转换为Mp4视频格式

我需要将OGV视频格式转换为MP4视频格式

  • 我使用下面的Java代码将MP4视频格式转换为OGV视频格式,工作正常
  • 对于MP4格式,我使用音频。setCodec(“AudioAttributes.DIRECT\u STREAM\u COPY”)和视频。setCodec(“VideoAttributes.DIRECT\u STREAM\u COPY”)
  • 但是如果我通过改变音频使用相同的代码。setCodec(“libvorbis”)和视频。setCodec(“mpeg4”)未从Ogv转换为Mp4格式。我还在我的示例程序中尝试了一些编解码器

  • 我无法从Ogv转换为Mp4格式

public class VideoConvert {
    public static void main(String[] args) throws IOException {
    File source = new File("D:\\readxml\\videoConvertorProject\\MP4toOGV\\mp4\\Sample1.ogv");
    File target = new File("D:\\readxml\\videoConvertorProject\\MP4toOGV\\ogv\\Sample2.mp4");
    AudioAttributes audio = new AudioAttributes();
    //audio.setCodec("libvorbis");
    audio.setCodec("libfaac");
    //audio.setCodec("libmp3lame");
    //audio.setCodec(AudioAttributes.DIRECT_STREAM_COPY);
    audio.setBitRate(new Integer(128000));
    audio.setSamplingRate(new Integer(44100));
    audio.setChannels(new Integer(2));
    VideoAttributes video = new VideoAttributes();
    video.setBitRate(new Integer(160000));
    video.setFrameRate(new Integer(15));
    //video.setSize(new VideoSize(176, 144));
    //video.setCodec("libtheora");
    video.setCodec("mpeg4");
    //video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setFormat("mp4");
    attrs.setAudioAttributes(audio);
    attrs.setVideoAttributes(video);
    Encoder encoder = new Encoder();
    try {
        encoder.encode(source, target, attrs);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InputFormatException e) {
        e.printStackTrace();
    } catch (EncoderException e) {
        e.printStackTrace();
    }
}

共 (2) 个答案

  1. # 1 楼答案

    我不完全确定您使用的是什么库,但MPEG-4容器格式支持多种不同的视频和音频编解码器

    我看到libfaac的音频,这让我假设您使用的是基于ffmpeg的东西

    对于视频,您可能需要H.264/MPEG-4 AVC,在这种情况下,您将使用libx264作为视频编码器。我从来没有见过其他的mpeg4视频编码格式

  2. # 2 楼答案

    我想,您需要JAVE(Java音频视频编码器)库,所以我更改了setCodecAudioAttributes。直接流拷贝

    public class VideoConvert {
    public static void main(String[] args) throws IOException {
    File source = new File("D:\\video\\mp4\\Sample.ogv");
    File target = new File("D:\\video\\ogv\\Sample.mp4");
    AudioAttributes audio = new AudioAttributes();
    audio.setCodec(AudioAttributes.DIRECT_STREAM_COPY);
    audio.setBitRate(new Integer(128000));
    audio.setSamplingRate(new Integer(44100));
    audio.setChannels(new Integer(2));
    VideoAttributes video = new VideoAttributes();
    video.setBitRate(new Integer(160000));
    video.setFrameRate(new Integer(15));
    video.setCodec("mpeg4");
    video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setFormat("mp4");
    attrs.setAudioAttributes(audio);
    attrs.setVideoAttributes(video);
    Encoder encoder = new Encoder();
    try {
        encoder.encode(source, target, attrs);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InputFormatException e) {
        e.printStackTrace();
    } catch (EncoderException e) {
        e.printStackTrace();
    }
    

    }