Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SeekableByteArrayOutputStream cannot be played #2280

Open
ferrymo opened this issue Sep 8, 2024 · 0 comments
Open

SeekableByteArrayOutputStream cannot be played #2280

ferrymo opened this issue Sep 8, 2024 · 0 comments

Comments

@ferrymo
Copy link

ferrymo commented Sep 8, 2024

version: 1.5.10

public static void main(String[] args) throws IOException {
        String filePath="C:\\Users\\ferrym\\Desktop\\scrm\\surmount\\video-watermark\\lron-man.mp4";
        File file=new File(filePath);
        // 启用 FFmpeg 日志输出
        FFmpegLogCallback.set();
        //抓取视频资源
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file);



        //
        Frame frame = null;
        FFmpegFrameRecorder recorder = null;
        String fileName = null;
        SeekableByteArrayOutputStream outputStream  = new SeekableByteArrayOutputStream();
        try{
            frameGrabber.start();
            Random random=new Random();
            fileName = file.getAbsolutePath() + random.nextInt(100)+".mp4";
            System.out.println("文件名-->>"+fileName);
            recorder = new FFmpegFrameRecorder(outputStream, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
            recorder.setFormat("mp4");
            recorder.setSampleRate(frameGrabber.getSampleRate());
            recorder.setFrameRate(frameGrabber.getFrameRate());
            recorder.setTimestamp(frameGrabber.getTimestamp());
            recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
            recorder.setVideoCodec(frameGrabber.getVideoCodec());
            recorder.start();
            int index=0;
            while (true){
                frame=frameGrabber.grabFrame();
                if(frame==null){
                    System.out.println("视频处理完成");
                    break;
                }
                //判断音频
                System.out.println("音频=="+(frame.samples ==null)+"视频=="+ (frame.image==null));
                //判断图片帧
                if(frame.image !=null){
                    IplImage iplImage = Java2DFrameUtils.toIplImage(frame);
                    BufferedImage buffImg= Java2DFrameUtils.toBufferedImage(iplImage);
                    Graphics2D graphics = buffImg.createGraphics();
                    graphics.setColor(Color.WHITE);
                    graphics.setFont(new Font("微软雅黑", Font.BOLD, 20));
                    graphics.drawString("------------scrm技术团队--------------",(iplImage.width()/2)-100,iplImage.height()/2);
                    graphics.dispose();
                    Frame newFrame = Java2DFrameUtils.toFrame(buffImg);
                    recorder.record(newFrame);
                }
                //设置音频
                if(frame.samples !=null){
                    recorder.recordSamples(frame.sampleRate,frame.audioChannels,frame.samples);
                }
                System.out.println("帧值="+index);
                index ++;
            }


          File test =  new File(fileName);
            FileUtil.writeBytes(outputStream.toByteArray(),test);
//            writeBytesToFile(outputStream.toByteArray(),test);

            recorder.close();
            frameGrabber.close();
//            tempFile.delete();
//            FileUtil.delete(fileName);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants