一、下载原视频
1.you-get
在安装好python3后,在cmd中输入下面命令进行you-get的下载。
pip3 install you-get
然后使用下面命令进行原视频的下载。
you-get https://www.bilibili.com/video/BV124411Q7iV
2.Windows捕获功能
该功能可直接用作将视频截取一小段一小段的,缺点是需要手工进行操作。
也可以使用ffmpeg的视频截取+格式转换功能
ffmpeg.exe -i dance.mp4 dance.gif
二、处理视频
1.使用下面的ffmpeg命令将截取的MP4文件的每一帧分割出来。
ffmpeg -i dance.mp4 dance%d.jpeg
分割后的图片默认在bin文件夹下。
2.将分割出来的图片转为ASCII码的形式。
这里使用Java脚本进行转换,脚本如下:
package com.xs.util.image;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.FileImageInputStream;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.imageio.plugins.gif.GIFImageReader;
import com.sun.imageio.plugins.gif.GIFImageReaderSpi;
import sun.misc.BASE64Encoder;
public class Image2ASCII{
static String ascii = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\\\"^`'.";
static String base = "@#&$%*o!;.";//小帅丶使用这个字符
/** 图片类型 */
private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
/**
* gif图片转gif字符图
* @param imagePath 原图路径
* @param outPath 输出图片的文件夹路径
* @throws IOException
*/
public static void loadGif(String imagePath, String outPath)
throws IOException {
File imageFile = new File(imagePath);
FileImageInputStream in = new FileImageInputStream(imageFile);
ImageReaderSpi readerSpi = new GIFImageReaderSpi();
GIFImageReader gifImageReader = new GIFImageReader(readerSpi);
gifImageReader.setInput(in);
int num = gifImageReader.getNumImages(true);
System.out.println(num);
BufferedImage[] bufferedImages = new BufferedImage[num];
for (int i = 0; i < num; i++) {
BufferedImage bi = gifImageReader.read(i);
bufferedImages[i] = txtToImage(bi, outPath + "out" + i + ".jpeg"); //每一帧都保存为图片
}
jpgToGif(bufferedImages,outPath + imagePath.substring(imagePath.length() - 6)+ "outGif.gif", 200);
}
/**
* 图片转字符再保存为图片
* @param bi 原图
* @param outPutPath
* @return BufferedImage
*/
public static BufferedImage txtToImage(BufferedImage bi, String outPutPath) {
File imageFile = new File(outPutPath);
if (!imageFile.exists()) {
try {
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println(width + " " + height);
int speed = 7;
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// 获取图像上下文
Graphics g = createGraphics(bufferedImage, width, height, speed);
// 图片中文本行高
final int Y_LINEHEIGHT = speed;
int lineNum = 1;
for (int i = miny; i < height; i += speed) {
for (int j = minx; j < width; j += speed) {
int pixel = bi.getRGB(j, i); // 下面三行代码将一个数字转换为RGB数字
int red = (pixel & 0xff0000) >> 16;
int green = (pixel & 0xff00) >> 8;
int blue = (pixel & 0xff);
float gray = 0.299f * red + 0.578f * green + 0.114f * blue;
int index = Math.round(gray * (base.length() + 1) / 255);
// char c = ascii.charAt((int) (gray / 255 * ascii.length()));
// char c = toChar((int) gray);
// char c = toChar(index);
String c = index >= base.length() ? " " : String.valueOf(base.charAt(index));
g.drawString(String.valueOf(c), j, i);
}
lineNum++;
}
g.dispose();
// 保存为jpg图片
FileOutputStream fos;
try {
fos = new FileOutputStream(imageFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
OutputStream out = encoder.getOutputStream();
BASE64Encoder base64Encoder = new BASE64Encoder();
encoder.encode(bufferedImage);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ImageFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bufferedImage;
}
/**
* 画板默认一些参数设置
* @param image 图片
* @param width 图片宽
* @param height 图片高
* @param size 字体大小
* @return
*/
private static Graphics createGraphics(BufferedImage image, int width,
int height, int size) {
Graphics g = image.createGraphics();
g.setColor(null); // 设置背景色
g.fillRect(0, 0, width, height);// 绘制背景
g.setColor(Color.BLACK); // 设置前景色
g.setFont(new Font("微软雅黑", Font.PLAIN, size)); // 设置字体
return g;
}
/**
* n张jpg转gif方法
* @param bufferedImages
* @param newPic
* @param playTime
*/
private static void jpgToGif(BufferedImage[] bufferedImages, String newPic,
int playTime) {
try {
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.setRepeat(0);
e.start(newPic);
for (int i = 0; i < bufferedImages.length; i++) {
e.setDelay(playTime); // 设置播放的延迟时间
e.addFrame(bufferedImages[i]); // 添加到帧中
}
e.finish();
} catch (Exception e) {
System.out.println("jpgToGif Failed:");
}
}
}
代码为转载原文链接如下,原文中还提供了微信小程序转码。
【原文链接】
原图:
ASCII码图:
3.将ASCII码图合成视频。
通过opencv模块实现图片合成视频,首先下载opencv。
pip install opencv-python
将图片文件夹的路径传入参数即可。
def charts2video(img_path, video_path):
"""将给定目录下的图片转成视频
Args:
img_path: 图片路径
video_path: 输出视频的路径和名称
Returns: 图片转成的视频
"""
images = os.listdir(img_path)
images.sort(key=lambda x: int(x[:-4])) # 以名称字符串的数字从小到大排序
fps = 12 # 帧数
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
im = Image.open(img_path + images[0])
video_writer = cv2.VideoWriter(video_path, fourcc, fps, im.size)
for img_i in images:
frame = cv2.imread(img_path + img_i)
print('开始将 ' + img_i + ' 加入视频\n')
video_writer.write(frame) # 注意:图片尺寸必须和视频尺寸一样,不然不会被加入视频中!!!
video_writer.release()
效果图: