gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:08:37 25 4
gpt4 key购买 nike

我需要将 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();
}
}

最佳答案

我想,你是 JAVE(Java 音频视频编码器)库,所以我更改了 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();
}

关于java - 如何使用Java将ogv视频文件转换成Mp4视频格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34989459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com