0

I need to download a video in the given link and save to my local directory. This is the code I used for that. And I'm using java nio package.

  public void usingNio() throws Exception { 
            ReadableByteChannel readChannel = Channels.newChannel(new URL("https://www.dailymotion.com/embed/video/x82n558").openStream()); 
            FileOutputStream fileOS = new FileOutputStream(newFile("C:\\Users\\Tecman\\Desktop\\Knowladge\\video.mp4")); 
            FileChannel writeChannel = fileOS.getChannel(); writeChannel.transferFrom(readChannel, 0, Long.MAX_VALUE);
     }

Above code saving video.mp4 file in the given directory but it can't play using a video player. Seems like video file is corrupted.

I have tried several example in the internet but no luck. I know there are tons of video downloading tools available in internet but my purpose is download a video file from any provided link and save it to the directory using java. What i'm doing wrong here any guidance, code segment, link or tip will be really helpful.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    That doesn't look like the URL of an mp4 file but rather the URL of a DailyMotion page that *contains* a video. – RealSkeptic Nov 08 '22 at 18:19
  • 1
    That is not so easy. A video portal has tons of Javascript, and it could be very hard to get the stream URL (if the web does not have a specific API for that). You can extract the URL with some browser plugins ( https://addons.mozilla.org/en-US/firefox/addon/video-downloadhelper/ ) but doing this with Java could be hard. – PeterMmm Nov 08 '22 at 18:39
  • Like others said, you need to scrap the page first to get the actual URL of the video itself before attempting to download it, how to find that URL depends on the platform/website. – Yoh Nov 08 '22 at 18:58
  • if you can even find it, and then if you're even allowed to access it directly. Websites that allow direct downloading tend to have APIs for the purpose, often ones that do put restrictions on downloads (number, bandwidth, who can access them, etc.). And for good reasons, as each download is a cost for them, and you're bypassing the ads and other revenue generators they present to normal viewers – jwenting Nov 08 '22 at 20:11
  • As mentioned by others, the URI you have in your sample code just points to a page that has some javascripts running that actually download a file called `488168396_mp4_h264_aac_hq.ts` in `video/mp2t` format. A simple GET request though will only donwload a couple of kilobytes and its the constant issuing of "ranged" GET requests that actually retrieves the whole file - segment per segment. Note however, that the actual filename is hidden in the javascript code to obfuscate the actual name. – Roman Vottner Nov 09 '22 at 00:47
  • 1
    Generally, you must **close** resources. The preferred way is to use [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). In this case, you can simplify the entire operation to `try(InputStream is = new URL("https://www.dailymotion.com/embed/video/x82n558").openStream()) { Files.copy(is, Paths.get("C:\\Users\\Tecman\\Desktop\\Knowladge\\video.mp4")); }` Of course, it doesn’t help if the URL doesn’t actually point to the video. – Holger Jun 13 '23 at 16:08

0 Answers0