视频批量转换成GIF

使用ws.schild工具包对视频和音频进行转码、裁剪以及提取操作。

1.引入依赖

        <!--视频转gif依赖-->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>3.0.1</version>
        </dependency>

2.代码实现

import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoInfo;
import ws.schild.jave.info.VideoSize;

import java.io.File;
import java.util.Arrays;

/**
 * @author cyh
 * @date 2022/11/12 16:34
 */
public class VideoToGIf {

    //输出格式
    private static final String outputFormat = "gif";

    /**
     * 获得转化后的文件名
     * @param sourceFilePath 源视频文件路径
     * @return java.lang.String
     * @author cyh
     * @date 2022/11/12 17:01
     */
    public static String getNewFileName(String sourceFilePath) {
        File source = new File(sourceFilePath);
        String fileName = source.getName().substring(0, source.getName().lastIndexOf("."));
        return fileName + "." + outputFormat;
    }

    /**
     * 转化音频格式
     * @param sourceFilePath 源视频文件路径
     * @param targetFilePath 目标gif文件路径
     * @return void
     * @author cyh
     * @date 2022/11/12 17:00
     */
    public static void transform(String sourceFilePath, String targetFilePath) {
        File source = new File(sourceFilePath);
        File target = new File(targetFilePath);
        try {
            //获得原视频的分辨率
            MultimediaObject mediaObject = new MultimediaObject(source);
            MultimediaInfo multimediaInfo = mediaObject.getInfo();
            VideoInfo videoInfo = multimediaInfo.getVideo();
            VideoSize sourceSize = videoInfo.getSize();
            //设置视频属性
            VideoAttributes video = new VideoAttributes();
            video.setCodec(outputFormat);
            //设置视频帧率 正常为10 ,值越大越流畅
            video.setFrameRate(10);
            //设置视频分辨率
            VideoSize targetSize = new VideoSize(sourceSize.getWidth() / 5, sourceSize.getHeight() / 5);
            video.setSize(targetSize);
            //设置转码属性
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setVideoAttributes(video);
            // 音频转换格式类
            Encoder encoder = new Encoder();
            encoder.encode(mediaObject, target, attrs);
            System.out.println("= =转换成功= =");
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量转化视频格式
     * @param sourceFolderPath 源视频文件夹路径
     * @param targetFolderPath 目标gif文件夹路径
     * @return void
     * @author cyh
     * @date 2022/11/12 17:00
     */
    public static void batchTransform(String sourceFolderPath, String targetFolderPath) {
        File sourceFolder = new File(sourceFolderPath);
        if (sourceFolder.list().length != 0) {
            Arrays.asList(sourceFolder.list()).forEach(e -> {
                transform(sourceFolderPath + "\\" + e, targetFolderPath + "\\" + getNewFileName(e));
            });
        }
    }

    public static void main(String[] args) {
        batchTransform("C:\\video", "C:\\video");
    }
}

最后的结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
批量将webp格式转换gif,可以使用多种方法和工具来实现。 一种常见的方法是使用Python编程语言中的Pillow库。Pillow是一个用于图像处理的强大库,可以用来读取、转换和保存图像文件。首先,你需要安装Pillow库,可以使用pip命令进行安装。 安装完毕后,你可以通过编写一个简单的Python脚本来实现批量转换。首先,需要导入Pillow库: ```python from PIL import Image import os ``` 然后,定义一个函数,用于将单个webp文件转换gif文件: ```python def convert_webp_to_gif(filepath, output_folder): # 打开webp文件 img = Image.open(filepath) # 创建输出文件的路径和文件名 output_filename = os.path.splitext(os.path.basename(filepath))[0] + ".gif" output_filepath = os.path.join(output_folder, output_filename) # 保存为gif文件 img.save(output_filepath, "gif") ``` 接下来,遍历指定的目录,将其中的所有webp文件转换gif文件: ```python def batch_convert_webp_to_gif(input_folder, output_folder): # 确保输出文件夹存在 os.makedirs(output_folder, exist_ok=True) # 遍历输入文件夹中的webp文件 for filename in os.listdir(input_folder): if filename.endswith(".webp"): filepath = os.path.join(input_folder, filename) convert_webp_to_gif(filepath, output_folder) ``` 最后,调用批量转换函数,将指定的webp文件夹转换gif文件夹: ```python input_folder = "input_folder_path" output_folder = "output_folder_path" batch_convert_webp_to_gif(input_folder, output_folder) ``` 通过以上的步骤,你就可以实现将批量webp格式的文件转换gif文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值