java 音视屏编码转换

github项目地址:https://github.com/a-schild/jave2
本人使用示例

1.引入依赖

        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-core</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-nativebin-linux64</artifactId>
            <version>3.3.1</version>
        </dependency>

2.示例

import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;

import java.io.File;

public class VideoTest {
    public static void main(String[] args) throws EncoderException {
        File source = new File("D:\\test\\a.mp4");
        File target = new File("D:\\test\\b.mp4");
        AudioAttributes audio = new AudioAttributes();
        //音频编码格式
        audio.setCodec("libmp3lame");
        audio.setBitRate(800000);
        audio.setChannels(1);

        VideoAttributes video = new VideoAttributes();
        //视频编码格式
        video.setCodec("libx264");
//        video.setBitRate(3200000);
        //数字设置小了,视频会卡顿 帧速率
//        video.setFrameRate(30);
        EncodingAttributes attrs = new EncodingAttributes();

        attrs.setVideoAttributes(video);
        Encoder encoder = new Encoder();
        MultimediaObject multimediaObject = new MultimediaObject(source);
        encoder.encode(multimediaObject, target, attrs);
    }
}
可以使用开源的Java库如"JCodec"或"FFmpeg"来进行视频的TS处理。 其中,JCodec是一个Java视频解码器和编码器,它支持多种格式的视频和音频,包括TS格式。你可以使用JCodec来分离TS流并提取其中的视频和音频数据。 另外,FFmpeg是一个非常强大的开源跨平台视频处理工具,它也可以用于处理TS流。你可以使用Java中的"ProcessBuilder"类来执行FFmpeg的命令行工具。 以下是一个使用JCodec来进行TS处理的示例代码: ```java import org.jcodec.api.JCodecException; import org.jcodec.api.specific.ContainerAdaptor; import org.jcodec.api.specific.MP4Adaptor; import org.jcodec.common.io.NIOUtils; import org.jcodec.containers.mps.MPSDemuxer; import org.jcodec.containers.mps.MPSDemuxer.PESPacket; import org.jcodec.containers.mps.MTSUtils; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; import java.nio.file.Paths; public class TSDemuxer { public static void main(String[] args) throws IOException, JCodecException { String filePath = "input.ts"; String videoOutPath = "output_video.ts"; String audioOutPath = "output_audio.ts"; // Create a demuxer for the input file ReadableByteChannel channel = NIOUtils.readableChannel(new File(filePath)); MPSDemuxer mpsDemuxer = new MPSDemuxer(channel); // Find the first video and audio streams int videoStreamIndex = -1; int audioStreamIndex = -1; for (int i = 0; i < mpsDemuxer.getNumStreams(); i++) { MPSDemuxer.StreamInfo info = mpsDemuxer.getStream(i); if (info.getCodec() != null && info.getCodec().startsWith("video/")) { videoStreamIndex = i; } else if (info.getCodec() != null && info.getCodec().startsWith("audio/")) { audioStreamIndex = i; } } // Create output files for video and audio Files.deleteIfExists(Paths.get(videoOutPath)); Files.deleteIfExists(Paths.get(audioOutPath)); Files.createFile(Paths.get(videoOutPath)); Files.createFile(Paths.get(audioOutPath)); // Create container adaptors for the output files ContainerAdaptor videoAdaptor = new MP4Adaptor(NIOUtils.writableChannel(new File(videoOutPath))); ContainerAdaptor audioAdaptor = new MP4Adaptor(NIOUtils.writableChannel(new File(audioOutPath))); // Process the input TS stream PESPacket pesPacket; while ((pesPacket = mpsDemuxer.nextFrame()) != null) { ByteBuffer data = pesPacket.getData(); int streamIndex = pesPacket.getStreamId(); // Write the video or audio data to the appropriate output file if (streamIndex == videoStreamIndex) { MTSUtils.writeSample(videoAdaptor, data, 0, pesPacket.getPts(), pesPacket.getDts(), true, null); } else if (streamIndex == audioStreamIndex) { MTSUtils.writeSample(audioAdaptor, data, 0, pesPacket.getPts(), pesPacket.getDts(), true, null); } } // Close the container adaptors videoAdaptor.finish(); audioAdaptor.finish(); } } ``` 这个示例代码使用JCodec来分离输入的TS流,并将其中的视频和音频数据写入到不同的输出文件中。你可以根据需要修改代码来适应你的具体需求。 除此之外,使用FFmpeg处理TS流的方式也类似,你可以调用FFmpeg的命令行工具来进行视频的分离和提取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值