Open ferrymo opened 2 months ago
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(); } }
version: 1.5.10