Error 0xc00d36c4当打开从HTML源下载的MP4时。

huangapple go评论65阅读模式
英文:

Error 0xc00d36c4 when open an mp4 downloaded from an html source

问题

public class Main {

    public static void main(String[] args) {
        try {
            String code = getCode("https://www.animesaturn.it/ep/Naruto-ITA-ep-2");
            URL url = new URL(getMP4Url(code));
            URLConnection connection = url.openConnection();
            // 添加请求头
            connection.addRequestProperty("Accept", "*/*");
            connection.addRequestProperty("Accept-Encoding", "identity;q=1, *;q=0");
            connection.addRequestProperty("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7");
            connection.addRequestProperty("Connection", "keep-alive");
            connection.addRequestProperty("Cookie", "__cfduid=d42d9ef9cb8f4049d77bf29806929f3591600593349; _ga=GA1.2.1851438234.1600593351; _gid=GA1.2.1325787872.1600593351; 494668b4c0ef4d25bda4e75c27de2817=ea1b335c-3d0b-40f7-bb25-b91ab367d256:1:2");
            connection.addRequestProperty("DNT", "1");
            connection.addRequestProperty("Host", "server9.animesaturn.it");
            connection.addRequestProperty("If-Range", "\"4046154-5ae57e25d5964\"");
            connection.addRequestProperty("Range", "bytes=4784128-13303807");
            connection.addRequestProperty("Referer", "https://www.animesaturn.it/watch?file=3ZqYJugfVUOq");
            connection.addRequestProperty("Sec-Fetch-Dest", "video");
            connection.addRequestProperty("Sec-Fetch-Mode", "no-cors");
            connection.addRequestProperty("Sec-Fetch-Site", "same-site");
            connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36");
            // 打开流并保存文件
            InputStream in = connection.getInputStream();
            OutputStream out = new BufferedOutputStream(new FileOutputStream("test.mp4"));
            for (int b; (b = in.read()) != -1; ) {
                out.write(b);
            }
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getMP4Url(String code) throws IOException {
        String contentPage = getContentPage(new URL("https://www.animesaturn.it/watch?file=" + code));
        int index = contentPage.indexOf(".mp4") + 4;
        contentPage = contentPage.substring(0, index);
        index = contentPage.lastIndexOf("\"") + 1;
        return contentPage.substring(index);
    }

    private static String getCode(String stringUrl) throws IOException {
        URL url = new URL(stringUrl);
        String contentPage = getContentPage(url);
        int index = contentPage.indexOf("watch?file=");
        contentPage = contentPage.substring(index);
        index = contentPage.indexOf("=") + 1;
        contentPage = contentPage.substring(index);
        index = contentPage.indexOf("\"");
        return contentPage.substring(0, index);
    }

    private static String getContentPage(URL url) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String ln = reader.readLine();
        StringBuilder text = new StringBuilder();
        while (ln != null) {
            text.append(ln).append("\n");
            ln = reader.readLine();
        }
        return text.toString();
    }
}

Note: The provided code is a Java program designed to download an MP4 file from a specific URL. Please make sure you have the necessary permissions to download and use the content from the given URL. Also, remember that the specific URLs and websites mentioned in the code might change over time, so ensure that the URLs are accurate and accessible.

英文:

I am trying to download an mp4 file of a web page via java, but when I go to save it on my device and then open it I get the error "0xc00d36c4" or the program says that the file is corrupted.
This happens to me both if I open it with VLC and with any other programs like "Film & TV by Microsoft".
I leave you here the code of my program.

public class Main {
public static void main(String[] args) {
try {
String code = getCode("https://www.animesaturn.it/ep/Naruto-ITA-ep-2");
URL url = new URL(getMP4Url(code));
URLConnection connection = url.openConnection();
//Add the Property
connection.addRequestProperty("Accept", "*/*");
connection.addRequestProperty("Accept-Encoding", "identity;q=1, *;q=0");
connection.addRequestProperty("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Cookie", "__cfduid=d42d9ef9cb8f4049d77bf29806929f3591600593349; _ga=GA1.2.1851438234.1600593351; _gid=GA1.2.1325787872.1600593351; 494668b4c0ef4d25bda4e75c27de2817=ea1b335c-3d0b-40f7-bb25-b91ab367d256:1:2");
connection.addRequestProperty("DNT", "1");
connection.addRequestProperty("Host", "server9.animesaturn.it");
connection.addRequestProperty("If-Range", "\"4046154-5ae57e25d5964\"");
connection.addRequestProperty("Range", "bytes=4784128-13303807");//13303807 1599067366709604
connection.addRequestProperty("Referer", "https://www.animesaturn.it/watch?file=3ZqYJugfVUOq");
connection.addRequestProperty("Sec-Fetch-Dest", "video");
connection.addRequestProperty("Sec-Fetch-Mode", "no-cors");
connection.addRequestProperty("Sec-Fetch-Site", "same-site");
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36");
//Open the stream and save the file
InputStream in = connection.getInputStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream("test.mp4"));
for (int b; (b = in.read()) != -1; ) {
out.write(b);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getMP4Url(String code) throws IOException {
String contentPage = getContentPage(new URL("https://www.animesaturn.it/watch?file=" + code));
//System.out.println(contentPage);
//System.exit(0);
int index = contentPage.indexOf(".mp4") + 4;
contentPage = contentPage.substring(0, index);
index = contentPage.lastIndexOf("\"") + 1;
return (contentPage.substring(index));
}
private static String getCode(String stringUrl) throws IOException {
URL url = new URL(stringUrl);
String contentPage = getContentPage(url);
//System.out.println(contentPage);
int index = contentPage.indexOf("watch?file=");
contentPage = contentPage.substring(index);
index = contentPage.indexOf("=") + 1;
contentPage = contentPage.substring(index);
index = contentPage.indexOf("\"");
return (contentPage.substring(0, index));
}
private static String getContentPage(URL url) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String ln = reader.readLine();
StringBuilder text = new StringBuilder();
while (ln != null) {
text.append(ln).append("\n");
ln = reader.readLine();
}
return text.toString();
}
}

Some ideas?

答案1

得分: 1

删除这两行代码,它就能正常工作:

connection.addRequestProperty("If-Range", "\"4046154-5ae57e25d5964\"");
connection.addRequestProperty("Range", "bytes=4784128-13303807");//13303807 1599067366709604

HTTP Range 标头会使服务器仅发送文件的一部分,而您可能需要整个文件。 If-Range 也是类似的东西。

如果没有这些标头,您的代码将下载一个正确的 20 分钟视频(64 兆字节)。

英文:

Remove these two lines and it will work:

connection.addRequestProperty("If-Range", "\"4046154-5ae57e25d5964\"");
connection.addRequestProperty("Range", "bytes=4784128-13303807");//13303807 1599067366709604

HTTP Range header makes servers to send only a part of a file, while you would need the entire one. If-Range is also something similar.

Without these headers your code will download a correct 20-minute video (64 megabytes).

huangapple
  • 本文由 发表于 2020年9月21日 23:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定