java 获取视频时长-----云服务地址视频

发布于:2022-11-29 ⋅ 阅读:(273) ⋅ 点赞:(0)
public static void main(String[] args) {
    Integer number=FileVideoTimeMs("http://hwkxqqd.oss-cn-hangzhou.aliyuncs.com/xqqdfile/20220727/084b283231ba4edeadb21a7249c5383c.mp4");
    System.out.print("******************: "+number+" :******************");
}

public static Integer FileVideoTimeMs(String fileName){
    Encoder encoder = new Encoder();
    long ms = 0;
    try {
        File file = getFileByUrl(fileName);
        MultimediaInfo m = encoder.getInfo(file);
        ms = m.getDuration();
        deleteFile(file);
    }catch (Exception e){
        e.printStackTrace();
    }
    int ss = 1000;
    int mi = ss * 60;
    int hh = mi * 60;
    int dd = hh * 24;
    long day = ms / dd;
    long hour = (ms - day * dd) / hh;
    long minute = (ms - day * dd - hour * hh) / mi;
    long second = (ms - day * dd - hour * hh - minute * mi) / ss;
    Integer timeMS = Math.toIntExact(hour * 3600 + minute * 60 + second);
    return timeMS;
}
public static File getFileByUrl(String url) throws IOException {
    File tmpFile = File.createTempFile("temp", ".tmp");//创建临时文件
    toBDFile(url, tmpFile.getCanonicalPath());
    return tmpFile;
}
public static void toBDFile(String urlStr, String bdUrl) throws IOException, UnknownHostException {
    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    DataInputStream in = new DataInputStream(conn.getInputStream());
    byte[] data = toByteArray(in);
    in.close();
    FileOutputStream out = new FileOutputStream(bdUrl);
    out.write(data);
    out.close();
}
public static byte[] toByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 4];
    int n = 0;
    while ((n = in.read(buffer)) != -1) {
        out.write(buffer, 0, n);
    }
    return out.toByteArray();
}
/**
 * 删除临时文件
 * @param files
 */
private static void deleteFile(File... files) {
    for (File file : files) {
        if (file.exists()) {
            file.delete();
        }
    }
}
本文含有隐藏内容,请 开通VIP 后查看