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

Tam*_*lan 4 java video ffmpeg

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

  • 我使用下面的 Java 代码将 MP4 视频格式转换为 OGV 视频格式,它工作正常。
  • 对于 MP4 格式,我使用 audio.setCodec(" AudioAttributes.DIRECT_STREAM_COPY") 和 video.setCodec("VideoAttributes.DIRECT_STREAM_COPY")。
  • 但是如果我通过更改 audio.setCodec(" libvorbis") 和 video.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我想,你是 JAVE(Java Audio Video Encoder)库,所以我改变了 setCodec AudioAttributes.DIRECT_STREAM_COPY

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();
}
Run Code Online (Sandbox Code Playgroud)

}