
com.qiniu.process.qoss.M3U8Manager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qsuits Show documentation
Show all versions of qsuits Show documentation
qiniu-suits is a efficient tools for qiniu api implemented by java8.
package com.qiniu.process.qoss;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.model.qdora.VideoTS;
import com.qiniu.storage.Configuration;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class M3U8Manager {
private Client client;
private String protocol;
final private List m3u8ContentTypes = new ArrayList(){{
add("application/x-mpegurl");
add("application/vnd.apple.mpegurl");
}};
public M3U8Manager() {
this.client = new Client();
this.protocol = "http";
}
public M3U8Manager(Configuration configuration) {
this.client = new Client(configuration);
}
public M3U8Manager(String protocol) {
this.client = new Client();
this.protocol = "https".equals(protocol)? "https" : "http";
}
public M3U8Manager(Configuration configuration, String protocol) {
this.client = new Client(configuration);
this.protocol = "https".equals(protocol)? "https" : "http";
}
private List getVideoTSList(BufferedReader bufferedReader, String domain) throws IOException {
List ret = new ArrayList<>();
String line;
float seconds = 0;
while ((line = bufferedReader.readLine()) != null) {
if (line.startsWith("#")) {
if (line.startsWith("#EXTINF:")) {
line = line.substring(8, line.indexOf(","));
seconds = Float.parseFloat(line);
}
continue;
}
String url = line.startsWith("http") ? line : protocol + "://" +
(line.startsWith("/") ? domain + line : domain + "/" + line);
if (line.endsWith(".m3u8")) {
List tsList = getVideoTSListByUrl(url);
ret.addAll(tsList);
} else {
ret.add(new VideoTS(url, seconds));
}
seconds = 0;
}
return ret;
}
public List getVideoTSListByUrl(String m3u8Url) throws IOException {
Response response = client.get(m3u8Url);
if (m3u8ContentTypes.contains(response.contentType())) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.bodyStream()));
List ret = getVideoTSList(bufferedReader, m3u8Url.substring(m3u8Url.indexOf("://") + 3,
m3u8Url.indexOf("/", 9)));
bufferedReader.close();
response.close();
return ret;
} else {
response.close();
// 说明不是 m3u8 文件
throw new IOException(m3u8Url + " 's content-type is " + response.contentType() + ", not a m3u8 type.");
}
}
public List getVideoTSListByFile(String domain, String m3u8FilePath) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(m3u8FilePath)));
List ret = getVideoTSList(bufferedReader, domain);
bufferedReader.close();
return ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy