-1

I need to download a video from a server (API). Right now I have something like this:

def download_video(url, where_to_store):
   response = requests.get(url, stream=True)
   if response.status_code == 200:
      with open(f"{local_video_path}", "wb") as file:
           for chunk in response.iter_content(chunk_size=1024*1024):
               file.write(chunk)
   else:
      return response.status_code

This code downloads the video alright, but does not include the sound. I have tried this with different videos but with the same result. Do I have to download the sound seperately?

Many thanks!

2
  • 7
    I don't see anything wrong with your code. requests does not have any logic that would allow it to strip video from sound. Use e.g. this link to verify that your code works: sample-videos.com/video321/mp4/720/big_buck_bunny_720p_2mb.mp4 If you can share the actual URL you have problems with, that could help us in resolving this. Or shout if the file from the URL I provided comes without sound. Commented Jun 21 at 13:45
  • 3
    Audio is embedded in the mp4 file, just like other video formats. It's most likely a problem with your playback device. Maybe as simple as forgetting you have headphones plugged in the audio jack.
    – Ed_
    Commented Jun 24 at 21:31

1 Answer 1

2
+50

"Do I have to download the sound seperately?"

Most likely Yes. There are some servers that offer different video resolutions (quality select) and such servers usually keep separate files for audio and video. This allows for smaller files since they do not repeat the exact same audio data for each separate video file.

For example: Youtube does not keep audio in 1080p videos. You can "tried this with different videos" but still no 1080p file will have audio. Possibly your server/API is doing the same thing.

The simplest way you may have overseen is to use the Internet Browser for loading and playing the video and a screen recorder software capable of recording the screen and the audio not only from microphone, but also from any other sound source as this one used by the video.

In other words trying to write own program in whatever programming language is not necessary to achieve the effect you want and the advantage of this approach is that any video you are able to run in your Internet browser (which usually exceeds the capabilities of single libraries as it combines all of the available ones to provide you with the best Internet experience possible) becomes downloadable as file.

And if you are really in need of automating it ... why not use user input automation software to deliver the key-presses and mouse clicks necessary to make it happen in the way you have tested yourself as successful while doing in manually?

5
  • I need to automate it because I am creating a pyqt program which will go through a list of urls, download each video, and then play each one. Commented Jun 26 at 18:38
  • @JohnCarter : On what system are you? I can help only with Linux (I don't support MS Windows). The issue with downloading is often that the servers refuse downloading when the request does not come from an Internet browser. Without the URL it is hard to guess what is the actual issue ... hence this general kind of answer. There are sure ways to fool the server and I mean to remember that there are on stackoverflow answers how to do that with sending appropriate data along with the request and then download chunk by chunk like the Internet browser does it over the time of the video.
    – oOosys
    Commented Jun 26 at 18:43
  • Url: api.olhar.media/videos/sample720_2.mp4 Commented Jun 26 at 18:47
  • Using your code and the URL I am getting the video which plays with sound ... I suggest you try another video player. The video server supports downloading in the video player interface in the browser, so there is no need to fool the server and the download seems to come with the audio embedded in the video file.
    – oOosys
    Commented Jun 26 at 18:57
  • 1
    YOU ARE A GENIUS! I tried playing the video in another player and guess what? Its working! Stupid from my side, but I really thank you for your help! Commented Jun 26 at 19:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.