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?
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).
User contributions licensed under CC BY-SA 3.0