如何把m3u8转换为mp4(java将mp4转换成m3u8)

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

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

如何把m3u8转换为mp4(java将mp4转换成m3u8)(1)

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

如何把m3u8转换为mp4(java将mp4转换成m3u8)(2)

下载解压后配置环境变量

如何把m3u8转换为mp4(java将mp4转换成m3u8)(3)

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

如何把m3u8转换为mp4(java将mp4转换成m3u8)(4)

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 abcd.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-d.jpeg -ss 表示开始时间 -t 表示共要多少时间。 ffmpeg -i input.mp4 -ss 00:00:20 -t 10 -r 1 -q:v 2 -f image2 pic-d.jpeg #FFMPEG 截取视频中的图片作为封面 ffmpeg -ss 3 -i input.mp4 -vf "select=gt(scene\,0.4)" -frames:v 5 -vsync vfr -vf fps=fps=1/600 outd.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/testd.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/testd.ts"); ProcessCmdUtil.processCmd(command); }

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页