概述
思路就是把视频抽帧 然后将图片转成字符画 通过窗口播放出来达到视频效果
参考链接1.
视频抽帧
代码示例:
package com.抽帧;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* @author Kari
* @date 2021年09月14日下午1:32
*/
public class VideoToImage {
/**
* 视频抽帧
*/
/**
* 将视频文件帧处理并以“jpg”格式进行存储。
* 依赖FrameToBufferedImage方法:将frame转换为bufferedImage对象
* videoFileName 视频名称
* videoPath 视频文件路径
* videoFramesPath 存放截取视频某一帧的图片
*/
public static String grabberVideoFramer(String videoFileName ,String videoPath ,String videoFramesPath) {
//最后获取到的视频的图片的路径
String videPicture = "";
//Frame对象
Frame frame = null;
//标识
int flag = 0;
try {
/*
获取视频文件
*/
FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(videoPath + "/" + videoFileName);
fFmpegFrameGrabber.start();
//获取视频总帧数
int ftp = fFmpegFrameGrabber.getLengthInFrames();
System.out.println("时长 " + ftp / fFmpegFrameGrabber.getFrameRate() / 60);
int i =0 ;
while (flag <= ftp) {
frame = fFmpegFrameGrabber.grabImage();
/*
对视频的第五帧进行处理
*/
if (frame != null) {
//文件绝对路径+名字
String fileName = videoFramesPath +String.valueOf(i++) + ".jpg";
//文件储存对象
File outPut = new File(fileName);
ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
videPicture += outPut.getName();
}
flag++;
}
fFmpegFrameGrabber.stop();
fFmpegFrameGrabber.close();
} catch (Exception E) {
E.printStackTrace();
}
return videPicture;
}
public static BufferedImage FrameToBufferedImage(Frame frame) {
//创建BufferedImage对象
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
return bufferedImage;
}
public static void videoToImage(String videoFileName, String videoPath, String videoFramesPath){
grabberVideoFramer(videoFileName,videoPath,videoFramesPath);
}
}
图片转字符画
代码示例:
package com.抽帧;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Kari
* @date 2021年09月14日下午1:33
*/
public class ImageToStr {
/**
* 图片转字符
* @param filePath
* @param targetPath
*/
public static void createAsciiPic(String filePath,String targetPath) {
final String base = "@#&$%*o!;.";// 字符串由复杂到简单
// final String base = "KSPksp;,";
try { //输出图片文件夹
File file = new File(filePath);
FileWriter fileWriter = null;
fileWriter = new FileWriter(targetPath);//创建文本文件
BufferedWriter bw = new BufferedWriter(fileWriter);
if (file.isDirectory()){
File[] files = file.listFiles();
System.out.println(files.length);
for (int i=0; i<files.length; i++) {
//System.out.println(files[i].getName());
BufferedImage image = ImageIO.read(files[i]);
for (int y = 0; y < image.getHeight(); y += 4) {
StringBuilder sss=new StringBuilder ( "" );
for (int x = 0; x < image.getWidth(); x+=2) {
final int pixel = image.getRGB(x, y);
final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
final float gray = 0.299f * r + 0.578f * g + 0.114f * b;
final int index = Math.round(gray * (base.length() + 1) / 255);
sss.append(index >= base.length() ? " " : String.valueOf(base.charAt(index)));
}
String s = sss.toString()+"\r\n";
bw.write(s);// 往已有的文件上添加字符串
}
}
}
bw.close();
fileWriter.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
窗口播放
package com.抽帧;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Kari
* @date 2021年09月14日下午1:34
*/
public class TextMove {
private static final int FRAME_WIDTH = 640; //窗口宽
private static final int FRAME_HEIGHT = 960;//窗口高
/**
* 字符动画
* @param filePath
* @throws IOException
* @throws InterruptedException
*/
public static void textMove(String filePath) throws IOException, InterruptedException {
//JFrame的相关内容自行学习
JFrame frame = new JFrame();
frame.setTitle("井川里予");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
frame.setResizable(false); //设置框架是否可由用户调整大小。
frame.setUndecorated(false); //禁用或启用此框架的装饰
JTextArea area = new JTextArea();
area.setBackground(Color.white);
area.setForeground(Color.RED);
area.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
area.setFont(new Font("微软雅黑",Font.PLAIN,12));
frame.add(area);
frame.setVisible(true);
File file =new File(filePath);
show(area,file);
}
/**
* 显示到窗口
* @param area
* @param file
* @throws IOException
* @throws InterruptedException
*/
public static void show(JTextArea area, File file) throws IOException, InterruptedException {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
int line = 0;
int page = 0;
String[] pageStr=new String[41];
while (line <= 4000 ) { //总行数
String str = br.readLine();
sb.append(str+"\r\n");
System.out.println(str);
line++;
if(line%200 == 0){ // 每帧的行数
page++;
//System.out.print(sb);
pageStr[page] = sb.toString();
sb = new StringBuffer();
area.setText(pageStr[page]);
Thread.sleep(50L); //视觉暂留
}
}
for(int j=0;j<1000;j++){
for(String sss : pageStr){
area.setText(sss);
Thread.sleep(25L);
}
}
}
}