我正在使用ffmpeg java库将捕获的屏幕截图转换为视频。视频输出模糊
我正在使用ffmpeg java库将捕获的屏幕截图转换为视频。作为输出生成的视频模糊 我使用的比特率为9000,每秒帧数为25,视频大小为桌面屏幕大小 关于如何解决这个问题的任何建议 另外,由于某些限制,我无法使用ffmpeg.exe和命令行,因此我选择使用ffmpeg java库 关于这个问题的任何建议或关于任何更好方法的建议都会有所帮助我正在使用ffmpeg java库将捕获的屏幕截图转换为视频。视频输出模糊,java,opencv,image-processing,video,ffmpeg,Java,Opencv,Image Processing,Video,Ffmpeg,我正在使用ffmpeg java库将捕获的屏幕截图转换为视频。作为输出生成的视频模糊 我使用的比特率为9000,每秒帧数为25,视频大小为桌面屏幕大小 关于如何解决这个问题的任何建议 另外,由于某些限制,我无法使用ffmpeg.exe和命令行,因此我选择使用ffmpeg java库 关于这个问题的任何建议或关于任何更好方法的建议都会有所帮助 import java.awt.AWTException; import java.awt.Dimension; import ja
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.OpenCVFrameConverter;
public class ScreenRecorder{
public static boolean videoComplete=false;
public static String inputImageDir="inputImgFolder"+File.separator;
public static String inputImgExt="png";
public static String outputVideo="recording.mp4";
public static int counter=0;
public static int imgProcessed=0;
public static FFmpegFrameRecorder recorder=null;
public static int videoWidth=1920;
public static int videoHeight=1080;
public static int videoFrameRate=3;
public static int videoQuality=0; // 0 is the max quality
public static int videoBitRate=9000;
public static String videoFormat="mp4";
public static int videoCodec=avcodec.AV_CODEC_ID_MPEG4;
public static Thread t1=null;
public static Thread t2=null;
public static JFrame frame=null;
public static boolean isRegionSelected=false;
public static int c1=0;
public static int c2=0;
public static int c3=0;
public static int c4=0;
public static void main(String[] args) {
try {
if(getRecorder()==null)
{
System.out.println("Cannot make recorder object, Exiting program");
System.exit(0);
}
if(getRobot()==null)
{
System.out.println("Cannot make robot object, Exiting program");
System.exit(0);
}
File scanFolder=new File(inputImageDir);
scanFolder.delete();
scanFolder.mkdirs();
createGUI();
} catch (Exception e) {
System.out.println("Exception in program "+e.getMessage());
}
}
public static void createGUI()
{
frame=new JFrame("Screen Recorder");
JButton b1=new JButton("Select Region for Recording");
JButton b2=new JButton("Start Recording");
JButton b3=new JButton("Stop Recording");
JLabel l1=new JLabel("<html><br/>If you dont select a region then full screen recording <br/> will be made when you click on Start Recording</html>");
l1.setFont (l1.getFont ().deriveFont (20.0f));
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(frame, "A new window will open. Use your mouse to select the region you like to record");
new CropRegion().getImage();
} catch (Exception e1) {
// TODO Auto-generated catch block
System.out.println("Issue while trying to call the module to crop region");
e1.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter=0;
startRecording();
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopRecording();
System.out.print("Exiting...");
System.exit(0);
}
});
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(l1);
frame.setLayout(new FlowLayout(0));
frame.setVisible(true);
frame.setSize(1000, 170);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void startRecording()
{
t1=new Thread()
{
public void run() {
try {
takeScreenshot(getRobot());
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Cannot make robot object, Exiting program "+e.getMessage());
System.out.println("Cannot make robot object, Exiting program "+e.getMessage());
System.exit(0);
}
}
};
t2=new Thread()
{
public void run() {
prepareVideo();
}
};
t1.start();
t2.start();
System.out.println("Started recording at "+new Date());
}
public static Robot getRobot() throws Exception
{
Robot r=null;
try {
r = new Robot();
return r;
} catch (AWTException e) {
JOptionPane.showMessageDialog(frame, "Issue while initiating Robot object "+e.getMessage());
System.out.println("Issue while initiating Robot object "+e.getMessage());
throw new Exception("Issue while initiating Robot object");
}
}
public static void takeScreenshot(Robot r)
{
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rec=new Rectangle(size);
if(isRegionSelected)
{
rec=new Rectangle(c1, c2, c3-c1, c4-c2);
}
while(!videoComplete)
{
counter++;
BufferedImage img = r.createScreenCapture(rec);
try {
ImageIO.write(img, inputImgExt, new File(inputImageDir+counter+"."+inputImgExt));
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Got an issue while writing the screenshot to disk "+e.getMessage());
System.out.println("Got an issue while writing the screenshot to disk "+e.getMessage());
counter--;
}
}
}
public static void prepareVideo()
{
File scanFolder=new File(inputImageDir);
while(!videoComplete)
{
File[] inputFiles=scanFolder.listFiles();
try {
getRobot().delay(500);
} catch (Exception e) {
}
//for(int i=0;i<scanFolder.list().length;i++)
for(int i=0;i<inputFiles.length;i++)
{
//imgProcessed++;
addImageToVideo(inputFiles[i].getAbsolutePath());
//String imgToAdd=scanFolder.getAbsolutePath()+File.separator+imgProcessed+"."+inputImgExt;
//addImageToVideo(imgToAdd);
//new File(imgToAdd).delete();
inputFiles[i].delete();
}
}
File[] inputFiles=scanFolder.listFiles();
for(int i=0;i<inputFiles.length;i++)
{
addImageToVideo(inputFiles[i].getAbsolutePath());
inputFiles[i].delete();
}
}
public static FFmpegFrameRecorder getRecorder() throws Exception
{
if(recorder!=null)
{
return recorder;
}
recorder = new FFmpegFrameRecorder(outputVideo,videoWidth,videoHeight);
try
{
recorder.setFrameRate(videoFrameRate);
recorder.setVideoCodec(videoCodec);
recorder.setVideoBitrate(videoBitRate);
recorder.setFormat(videoFormat);
recorder.setVideoQuality(videoQuality); // maximum quality
recorder.start();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(frame, "Exception while starting the recorder object "+e.getMessage());
System.out.println("Exception while starting the recorder object "+e.getMessage());
throw new Exception("Unable to start recorder");
}
return recorder;
}
public static OpenCVFrameConverter.ToIplImage getFrameConverter()
{
OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
return grabberConverter;
}
public static void addImageToVideo(String imgPath)
{
try {
getRecorder().record(getFrameConverter().convert(cvLoadImage(imgPath)));
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Exception while adding image to video "+e.getMessage());
System.out.println("Exception while adding image to video "+e.getMessage());
}
}
public static void stopRecording()
{
try {
videoComplete=true;
System.out.println("Stopping recording at "+new Date());
t1.join();
System.out.println("Screenshot thread complete");
t2.join();
System.out.println("Video maker thread complete");
getRecorder().stop();
System.out.println("Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());
JOptionPane.showMessageDialog(frame, "Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());
} catch (Exception e) {
System.out.println("Exception while stopping the recorder "+e.getMessage());
}
}
}
CropRegion.java
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CropRegion implements MouseListener,
MouseMotionListener {
int drag_status = 0;
int c1;
int c2;
int c3;
int c4;
JFrame frame=null;
static int counter=0;
JLabel background=null;
public void getImage() throws AWTException, IOException, InterruptedException {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
ImagePanel panel = new ImagePanel(img);
frame=new JFrame();
frame.add(panel);
frame.setLocation(0, 0);
frame.setSize(size);
frame.setLayout(new FlowLayout());
frame.setUndecorated(true);
frame.setVisible(true);
frame.addMouseListener(this);
frame.addMouseMotionListener(this);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void draggedScreen() throws Exception {
ScreenRecorder.c1=c1;
ScreenRecorder.c2=c2;
ScreenRecorder.c3=c3;
ScreenRecorder.c4=c4;
ScreenRecorder.isRegionSelected=true;
JOptionPane.showMessageDialog(frame, "Region Selected.Please click on Start Recording button to record the selected region.");
frame.dispose();
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
paint();
this.c1 = arg0.getX();
this.c2 = arg0.getY();
}
public void mouseReleased(MouseEvent arg0) {
paint();
if (this.drag_status == 1) {
this.c3 = arg0.getX();
this.c4 = arg0.getY();
try {
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void mouseDragged(MouseEvent arg0) {
paint();
this.drag_status = 1;
this.c3 = arg0.getX();
this.c4 = arg0.getY();
}
public void mouseMoved(MouseEvent arg0) {
}
public void paint() {
Graphics g = frame.getGraphics();
frame.repaint();
int w = this.c1 - this.c3;
int h = this.c2 - this.c4;
w *= -1;
h *= -1;
if (w < 0) {
w *= -1;
}
g.drawRect(this.c1, this.c2, w, h);
}
}
import java.awt.AWTException;
导入java.awt.Dimension;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.Rectangle;
导入java.awt.Robot;
导入java.awt.Toolkit;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseMotionListener;
导入java.awt.image.buffereImage;
导入java.io.IOException;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
公共类CropRegion实现MouseListener,
MouseMotionListener{
int drag_status=0;
int c1;
int c2;
int c3;
int-c4;
JFrame=null;
静态整数计数器=0;
JLabel背景=null;
public void getImage()引发AWTException、IOException、interruptedeexception{
维度大小=Toolkit.getDefaultToolkit().getScreenSize();
机器人=新机器人();
BuffereImage img=robot.createScreenCapture(新矩形(大小));
ImagePanel=新的ImagePanel(img);
frame=新的JFrame();
框架。添加(面板);
帧设置位置(0,0);
框架。设置尺寸(尺寸);
frame.setLayout(新的FlowLayout());
框架。设置未装饰(真实);
frame.setVisible(true);
frame.addMouseListener(本);
frame.addMouseMotionListener(此);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void draggedScreen()引发异常{
屏幕记录器c1=c1;
屏幕记录器。c2=c2;
屏幕记录器。c3=c3;
屏幕记录器。c4=c4;
ScreenRecorder.isRegionSelected=true;
JOptionPane.showMessageDialog(框,“所选区域。请单击开始录制按钮录制所选区域”);
frame.dispose();
}
公共无效鼠标单击(鼠标事件arg0){
}
公共无效鼠标事件(鼠标事件arg0){
}
public void mouseexitted(MouseEvent arg0){
}
public void mousePressed(MouseEvent arg0){
油漆();
this.c1=arg0.getX();
this.c2=arg0.getY();
}
公共无效MouseEvent arg0{
油漆();
if(this.drag_status==1){
this.c3=arg0.getX();
this.c4=arg0.getY();
试一试{
DragedScreen();
}捕获(例外e){
e、 printStackTrace();
}
}
}
公共无效鼠标标记(鼠标事件arg0){
油漆();
this.drag_status=1;
this.c3=arg0.getX();
this.c4=arg0.getY();
}
public void mouseMoved(MouseEvent arg0){
}
公共空间涂料(){
Graphics g=frame.getGraphics();
frame.repaint();
int w=this.c1-this.c3;
int h=this.c2-this.c4;
w*=-1;
h*=-1;
if(w<0){
w*=-1;
}
g、 drawRect(this.c1,this.c2,w,h);
}
}
能否请您更新问题以显示用于转换的主代码,这可能有助于诊断。我已添加了完整的代码。我看不到任何ffmpeg代码。如何将图像转换为视频?如果在转换图像之前图像模糊,则问题不在于ffmpeg。编辑:请更新您的问题以显示格式化代码。由于您将其全部粘贴在一行中,我们无法区分什么是注释,什么是代码。很抱歉造成混淆。请在github链接中查看完整的代码抱歉,但是问题不允许链接到github或其他非现场代码存储库,相反,所有相关的代码都应该以代码格式文本的形式发布在问题中,最好是以有效文本的形式发布(请阅读将解释此结构的链接,如何制作,以及为什么这样做可以帮助您获得更好更快的答案)。
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CropRegion implements MouseListener,
MouseMotionListener {
int drag_status = 0;
int c1;
int c2;
int c3;
int c4;
JFrame frame=null;
static int counter=0;
JLabel background=null;
public void getImage() throws AWTException, IOException, InterruptedException {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
ImagePanel panel = new ImagePanel(img);
frame=new JFrame();
frame.add(panel);
frame.setLocation(0, 0);
frame.setSize(size);
frame.setLayout(new FlowLayout());
frame.setUndecorated(true);
frame.setVisible(true);
frame.addMouseListener(this);
frame.addMouseMotionListener(this);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void draggedScreen() throws Exception {
ScreenRecorder.c1=c1;
ScreenRecorder.c2=c2;
ScreenRecorder.c3=c3;
ScreenRecorder.c4=c4;
ScreenRecorder.isRegionSelected=true;
JOptionPane.showMessageDialog(frame, "Region Selected.Please click on Start Recording button to record the selected region.");
frame.dispose();
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
paint();
this.c1 = arg0.getX();
this.c2 = arg0.getY();
}
public void mouseReleased(MouseEvent arg0) {
paint();
if (this.drag_status == 1) {
this.c3 = arg0.getX();
this.c4 = arg0.getY();
try {
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void mouseDragged(MouseEvent arg0) {
paint();
this.drag_status = 1;
this.c3 = arg0.getX();
this.c4 = arg0.getY();
}
public void mouseMoved(MouseEvent arg0) {
}
public void paint() {
Graphics g = frame.getGraphics();
frame.repaint();
int w = this.c1 - this.c3;
int h = this.c2 - this.c4;
w *= -1;
h *= -1;
if (w < 0) {
w *= -1;
}
g.drawRect(this.c1, this.c2, w, h);
}
}