使用大名鼎鼎的ffmpeg,把视频文件切片成m3u8,并且通过springboot,可以实现在线的点播。
想法
客户端上传视频到服务器,服务器对视频进行切片后,返回m3u8,封面等访问路径。可以在线的播放。
服务器可以对视频做一些简单的处理,例如裁剪,封面的截取时间。
视频转码文件夹的定义
喜羊羊与灰太狼 // 文件夹名称就是视频标题
|-index.m3u8 // 主m3u8文件,里面可以配置多个码率的播放地址
|-poster.jpg // 截取的封面图片
|-ts // 切片目录
|-index.m3u8 // 切片播放索引
|-key // 播放需要解密的AES KEY
实现
需要先在本机安装FFmpeg,并且添加到PATH环境变量,如果不会先通过搜索引擎找找资料
工程
pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置文件
server:
port: 80
app:
# 存储转码视频的文件夹地址
video-folder: "C:\\Users\\Administrator\\Desktop\\tmp"
spring:
servlet:
multipart:
enabled: true
# 不限制文件大小
max-file-size: -1
# 不限制请求体大小
max-request-size: -1
# 临时IO目录
location: "${java.io.tmpdir}"
# 不延迟解析
resolve-lazily: false
# 超过1Mb,就IO到临时目录
file-size-threshold: 1MB
web:
resources:
static-locations:
- "classpath:/static/"
- "file:${
app.video-folder}" # 把视频文件夹目录,添加到静态资源目录列表
TranscodeConfig,用于控制转码的一些参数
package com.demo.ffmpeg;
public class TranscodeConfig {
private String poster; // 截取封面的时间 HH:mm:ss.[SSS]
private String tsSeconds; // ts分片大小,单位是秒
private String cutStart; // 视频裁剪,开始时间 HH:mm:ss.[SSS]
private String cutEnd; // 视频裁剪,结束时间 HH:mm:ss.[SSS]
public String getPoster() {
return poster;
}
public void setPoster(String poster) {
this.poster = poster;
}
public String getTsSeconds() {
return tsSeconds;
}
public void setTsSeconds(String tsSeconds) {
this.tsSeconds = tsSeconds;
}
public String getCutStart() {
return cutStart;
}
public void setCutStart(String cutStart) {
this.cutStart = cutStart;
}
public String getCutEnd() {
return cutEnd;
}
public void setCutEnd(String cutEnd) {
this.cutEnd = cutEnd;
}
@Override
public String toString() {
return "TranscodeConfig [poster=" + poster + ", tsSeconds=" + tsSeconds + ", cutStart=" + cutStart + ", cutEnd="
+ cutEnd + "]";
}
}
MediaInfo,封装视频的一些基础信息
package com.demo.ffmpeg;
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class MediaInfo {
public static class Format {
@SerializedName("bit_rate")
private String bitRate;
public String getBitRate() {
return bitRate;
}
public void setBitRate(String bitRate) {
this.bitRate = bitRate;
}
}
public static class Stream {
@SerializedName("index")
private int index;
@SerializedName("codec_name")
private String codecName;
@SerializedName("codec_long_name")
private String codecLongame;
@SerializedName("profile")
private String profile;
}
// ----------------------------------
@SerializedName("streams")
private List<Stream> streams;
@SerializedName("format")
private Format format;
public List<Stream> getStreams() {
return streams;
}
public void setStreams(List<Stream> streams) {
this.streams = streams;
}
public Format getFormat() {
return format;
}
public void setFormat(Format format) {
this.format = format;
}
}
FFmpegUtils,工具类封装FFmpeg的一些操作
package com.demo.ffmpeg;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.KeyGenerator;
import org.apache.commons.codec