图片转字符图片(三)

简介: 视频转字符视频

序言

这个是从抖音上学来的,一开始刷抖音,遇到不少字符串跳舞的视频,因此来实践一下

主要分为三个部分

  1. 静态图片转静态图片
  2. gif转gif
  3. 视频转视频

视频转视频

主要用到了FFmpeg这个工具,利用命令对视频文件进行操作。首先根据自己调的参数进行图片的截取(本文的是1秒10帧的参数),图片转换,然后分离音频,最后字符图片和音频合成目标视频。

FFmpeg的代码库:
https://github.com/FFmpeg/FFmpeg
FFmpeg下载地址:
https://ffmpeg.org/download.html

主要用到的几个命令,其他按帧截图命令参考文末链接4:

// 截图
ffmpeg -ss 10 -i input.flv -y -f image2  -vframes 100 -s 352x240 b-%03d.jpg
// 分离音频
ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac
// 合成视频
ffmpeg -threads2 -y -r 10 -i /tmpdir/image%04d.jpg -i audio.mp3 -absf aac_adtstoasc output.mp4

环境:
JDK 1.8

    // ffmpeg -ss 10 -i input.flv -y -f image2  -vframes 100 -s 352x240 b-%03d.jpg  
    /**
     * ffmpeg 截图,并指定图片的大小
     * 
     * @param srcVideoPath
     * @param tarImagePath
     *            截取后图片路径
     * @param width
     *            截图的宽
     * @param hight
     *            截图的高
     * @param offsetValue
     *            表示相对于文件开始处的时间偏移值 可以是分秒
     * @param vframes
     *            表示截图的桢数
     * 
     * @return
     */
    public static boolean processFfmpegImage(String srcVideoPath, String tarImagePath, int width, int hight,
            float offsetValue, float vframes) {
        if (!checkfile(srcVideoPath)) {
            System.out.println("【" + srcVideoPath + "】  不存在 !");
            // logger.error("【" + srcVideoPath + "】 不存在 !");
            return false;
        }
        List<String> commend = new java.util.ArrayList<String>();

        commend.add(ffmpegPath);

        commend.add("-i");

        commend.add(srcVideoPath);

        commend.add("-y");

        commend.add("-f");

        commend.add("image2");

        commend.add("-ss");

        commend.add(offsetValue + ""); // 在视频的某个插入时间截图,例子为5秒后

        // commend.add("-vframes");

        commend.add("-t");// 添加参数"-t",该参数指定持续时间

        commend.add(vframes + ""); // 截图的桢数,添加持续时间为1毫秒

        commend.add("-s");

        commend.add(width + "x" + hight); // 截图的的大小

        commend.add(tarImagePath);

        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
            // builder.redirectOutput(new File("F:/123/log/log.log"));
            Process process = builder.start();
            doWaitFor(process);
            process.destroy();
            if (!checkfile(tarImagePath)) {
                System.out.println(tarImagePath + " is not exit!  processFfmpegImage 转换不成功 !");
                // logger.info(tarImagePath + " is not exit! processFfmpegImage
                // 转换不成功 !");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("【" + srcVideoPath + "】 processFfmpegImage  转换不成功 !");
            // logger.error("【" + srcVideoPath + "】 processFfmpegImage 转换不成功
            // !");
            return false;
        }
    }
    
    public static boolean processFfmpegAudio(String srcVideoPath, String tarAudioPath) {
        if (!checkfile(srcVideoPath)) {
            System.out.println("【" + srcVideoPath + "】  不存在 !");
            // logger.error("【" + srcVideoPath + "】 不存在 !");
            return false;
        }
        // https://blog.csdn.net/xiaocao9903/article/details/53420519
        // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac
        // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.m4a
        
        List<String> commend = new java.util.ArrayList<String>();
        
        commend.add(ffmpegPath);
        
        commend.add("-i");
        
        commend.add(srcVideoPath);
        
        commend.add("-vn");

        commend.add("-y");
        
        commend.add("-acodec");
        
        commend.add("copy"); // 在视频的某个插入时间截图,例子为5秒后
        
        commend.add(tarAudioPath);
        
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
            Process process = builder.start();
            doWaitFor(process);
            process.destroy();
            if (!checkfile(tarAudioPath)) {
                System.out.println(tarAudioPath + " is not exit!  processFfmpegAudio 转换不成功 !");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("【" + srcVideoPath + "】 processFfmpegAudio  转换不成功 !");
            return false;
        }
    }

    /**
     * ffmpeg 合成视频
     * 
     * @param srcVideoPath
     * @param tarImagePath
     *            截取后图片路径
     * @param width
     *            截图的宽
     * @param hight
     *            截图的高
     * @param offsetValue
     *            表示相对于文件开始处的时间偏移值 可以是分秒
     * @param vframes
     *            表示截图的桢数
     * 
     * @return
     */
    public static boolean processFfmpegVideo(String imagePath, String audioPath, String tarVideoPath, int step) {
        // https://blog.csdn.net/wangshuainan/article/details/77914508?fps=1&locationNum=4
        // 带音频
        // ffmpeg -threads2 -y -r 10 -i /tmpdir/image%04d.jpg -i audio.mp3 -absf
        // aac_adtstoasc output.mp4

        List<String> commend = new java.util.ArrayList<String>();

        commend.add(ffmpegPath);

        commend.add("-threads");
        
        commend.add("2");

        commend.add("-y");

        commend.add("-r");

        commend.add(step + "");

        commend.add("-i");

        commend.add(imagePath); // 图片

        commend.add("-i");

         commend.add(audioPath);

        commend.add("-absf");// 

        commend.add("aac_adtstoasc"); // 

        commend.add(tarVideoPath);

        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
             builder.redirectOutput(new File("F:/123/log/log.log"));
            Process process = builder.start();
            doWaitFor(process);
            process.destroy();
            if (!checkfile(tarVideoPath)) {
                System.out.println(tarVideoPath + " is not exit!  processFfmpegVideo 转换不成功 !");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("【" + tarVideoPath + "】 processFfmpegVideo  转换不成功 !");
            return false;
        }
    }

源码地址:
https://github.com/Ruffianjiang/java4fun/tree/master/img2text

参考:

  1. https://blog.csdn.net/i_likechard/article/details/79032931
  2. https://blog.csdn.net/xiaocao9903/article/details/53420519
  3. https://blog.csdn.net/wangshuainan/article/details/77914508?fps=1&locationNum=4
  4. https://blog.csdn.net/yourijing/article/details/50786758
目录
相关文章
|
7月前
|
C#
C# 截取图片斜距形
C# 截取图片斜距形 需要:从一张大图中截取出某一区域的图片 前提:.Net framework 4.8 及以上 示例代码:private static void ImageCutRectangle() { // 打开待截取的大图 using (Image seatImg = Image.FromFile("4.jpg")) ...
51 0
C# 截取图片斜距形
|
4天前
|
存储 计算机视觉 Python
python实现Gif图片的字符画
这是一个Python实战项目,旨在将GIF动态图转化为ASCII字符动画。项目适合有一定Python基础的学习者,主要使用os、imageio、PIL库。首先,代码导入所需库,然后通过PIL创建空白图片并添加文本。接着,程序读取GIF,拆分帧并转为字符画,存入“tmp”目录。同时,代码提供了清空“tmp”目录、将灰度值映射为ASCII字符、将图片处理成字符画的函数。此外,还有创建新画布和合成GIF的步骤。主函数调用这些模块,最终将ASCII字符画合并成GIF。项目展示了将动态图像转换为ASCII艺术的过程。
19 0
|
2月前
|
文字识别 数据挖掘 网络安全
Python实现avif图片转jpg格式并识别图片中的文字
在做数据分析的时候有些数据是从图片上去获取的,这就需要去识别图片上的文字。Python有很多库可以很方便的实现OCR识别图片中的文字。这里介绍用EasyOCR库进行图片文字识别。easyocr是一个比较流行的库,支持超过80种语言,识别率高,速度也比较快。
31 2
|
12月前
|
编解码 开发工具 Android开发
.9图片的那点事儿
.9图片的那点事儿
146 0
如何将本地图片转成图片链接
如何将本地图片转成图片链接
1497 0
如何将本地图片转成图片链接
|
Java 索引
图片转字符图片(一)
图片转字符图片
1796 0
|
Java 索引
图片转字符图片(二)
gif 转字符 gif
1511 0
|
前端开发 Android开发 数据安全/隐私保护
Android图片添加文字水印并保存水印文字图片到指定文件
Android图片添加文字水印并保存水印文字图片到指定文件 package zhangphil.test; import android.
2900 0