一、下载原视频
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;
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);
}
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);
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);
String c = index >= base.length() ? " " : String.valueOf(base.charAt(index));
g.drawString(String.valueOf(c), j, i);
}
lineNum++;
}
g.dispose();
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;
}
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;
}
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()
效果图:
|