java将mp4转换成m3u8

一、在windos和ubuntu中配置安装ffmpeg

windows安装ffmpeg官方下载地址:Download FFmpeg

 点击后进入github选择此版本进行下载

下载解压后配置环境变量

 打开cmd输入ffmpeg -version命令,如下是安装成功

ubuntu安装ffmpeg

apt-get install yasm  #安装yasm
wget http://www.ffmpeg.org/releases/ffmpeg-4.1.6.tar.gz
tar -xvf ffmpeg-4.1.6.tar.gz
cd ffmpeg-4.1.6/
./configure && make && make install
ffmpeg -version   # 查看版本号

常用参数和命令

#主要参数
-i 设定输入流 
-f 设定输出格式 
-ss 开始时间 
#视频参数
-b 设定视频流量(码率),默认为 200Kbit/s 
-r 设定帧速率,默认为 25 
-s 设定画面的宽与高 
-aspect 设定画面的比例 
-vn 不处理视频 
-vcodec 设定视频编解码器,未设定时则使用与输入流相同的编解码器 
#音频参数
-ar 设定采样率 
-ac 设定声音的 Channel 数 
-acodec 设定声音编解码器,未设定时则使用与输入流相同的编解码器 
-an 不处理音频
#制作 M3U8 视频
#先用 ffmpeg 把 abc.mp4 文件转换为 abc.ts 文件
ffmpeg -y -i abc.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb abc.ts
#再用 ffmpeg 把 abc.ts 文件切片并生成 playlist.m3u8 文件,5 秒一个切片
ffmpeg -i abc.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 5 abc%03d.ts
 
#为视频添加 logo
ffmpeg -i input.mp4 -i iQIYI_logo.png -filter_complex overlay output.mp4
 
#将输入的 1920x1080 缩小到 960x540 输出
ffmpeg -i input.mp4 -vf scale=960:540 output.mp4
 
#抓取视频的一些帧,存为 jpeg 图片
#-r 表示每一秒几帧
#-q:v 表示存储 jpeg 的图像质量,一般 2 是高质量。
ffmpeg -i input.mp4 -r 1 -q:v 2 -f image2 pic-%03d.jpeg
-ss 表示开始时间
-t 表示共要多少时间。
ffmpeg -i input.mp4 -ss 00:00:20 -t 10 -r 1 -q:v 2 -f image2 pic-%03d.jpeg
 
#FFMPEG 截取视频中的图片作为封面
ffmpeg -ss 3 -i input.mp4 -vf "select=gt(scene\,0.4)" -frames:v 5 -vsync vfr -vf fps=fps=1/600 out%02d.jpg
 
#FFMPEG 截取指定时长的视频
ffmpeg -i input.avi -vcodec copy -acodec copy -ss 00:30:00 -t 00:30:00 output2.avi
 
#常用
ffmpeg -i G:/m3u8/demo.mp4 -c:v copy -c:a copy -f ssegment -segment_format mpegts -segment_list G:/m3u8/hls/test.m3u8 -segment_time 10 G:/m3u8/hls/test%05d.ts

二、java在windows中将mp4转换成m3u8

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ProcessCmdUtil {

    public static String processCmd(List<String> command) {
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(command);
            Process p = builder.start();
            int i = doWaitFor(p);
            p.destroy();
            return "成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
    }
    /**
     * 监听ffmpeg运行过程
     * @param p
     * @return
     */
    public static  int doWaitFor(Process p) {
        InputStream in = null;
        InputStream err = null;
        int exitValue = -1; // returned to caller when p is finished
        try {
            System.out.println("comeing");
            in = p.getInputStream();
            err = p.getErrorStream();
            boolean finished = false; // Set to true when p is finished

            while (!finished) {
                try {
                    while (in.available() > 0) {
                        Character c = new Character((char) in.read());
                        System.out.print(c);
                    }
                    while (err.available() > 0) {
                        Character c = new Character((char) err.read());
                        System.out.print(c);
                    }

                    exitValue = p.exitValue();
                    finished = true;

                } catch (IllegalThreadStateException e) {
                    Thread.currentThread().sleep(500);
                }
            }
        } catch (Exception e) {
            System.err.println("doWaitFor();: unexpected exception - "
                    + e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }

            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            if (err != null) {
                try {
                    err.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return exitValue;
    }

}
public static void main(String[] args) {
        String input = "G:/m3u8/demo.mp4";
        String output = "G:/m3u8/hls/test.m3u8";
        List<String> command = new ArrayList<>();
        command.add("ffmpeg");
        command.add("-i");
        command.add(input);
        command.add("-c:v");
        command.add("copy");
        command.add("-c:a");
        command.add("copy");
        command.add("-f");
        command.add("ssegment");
        command.add("-segment_format");
        command.add("mpegts");
        command.add("-segment_list");
        command.add(output);
        command.add("-segment_time");
        command.add("10");
        command.add("G:/m3u8/hls/test%05d.ts");
        ProcessCmdUtil.processCmd(command);
    }

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值