How to encode a video response in python? - python

In a request that I made I received a byte response and I know it is a response of a video. and it's status code was 200. And I don't know how to use it. I mean I tried to encode it into utf-8 and then save it to a file but it is not playable. media players is unable to read it's content here's the request that I made
import requests
resp = requests.get('https://bcboltsony-a.akamaihd.net/media/v1/hls/v4/aes128/5182475815001/4ded6ac4-6f8b-4da2-8194-db2391d5e331/164fe5c5-15a3-4997-b4c6-7dd4b95f9c57/92410c6d-c565-4341-8650-1d40a795ece2/5x/segment1.ts?akamai_token=exp=1589337578~acl=/media/v1/hls/v4/aes128/5182475815001/4ded6ac4-6f8b-4da2-8194-db2391d5e331/164fe5c5-15a3-4997-b4c6-7dd4b95f9c57/92410c6d-c565-4341-8650-1d40a795ece2/*~hmac=bf9745f2a9b51c04d59eb9955de20dcf1b4c8c7e434ad0bdd639f2d80fa10ecc')
open('E:/video.mp4', 'wb').write(bytes(resp.text, encoding='utf-8'))
how to convert this response to a watchable format

Try using wget which can help download files 10x easier.
Here is a simple code with your situation:
import wget
url = "https://bcboltsony-a.akamaihd.net/media/v1/hls/v4/aes128/5182475815001/4ded6ac4-6f8b-4da2-8194-db2391d5e331/164fe5c5-15a3-4997-b4c6-7dd4b95f9c57/92410c6d-c565-4341-8650-1d40a795ece2/5x/segment1.ts?akamai_token=exp=1589337578~acl=/media/v1/hls/v4/aes128/5182475815001/4ded6ac4-6f8b-4da2-8194-db2391d5e331/164fe5c5-15a3-4997-b4c6-7dd4b95f9c57/92410c6d-c565-4341-8650-1d40a795ece2/*~hmac=bf9745f2a9b51c04d59eb9955de20dcf1b4c8c7e434ad0bdd639f2d80fa10ecc"
wget.download(url, 'c:/users/Yourname/downloads/video.mp4')
If this does not work the problem of encoding may be on the url's side.

Your code is absolutely right.But note that:
If you open this page in your explorer,you will find it is a .ts file instead of .mp4 file.
Also,if you download it in the explorer directly, you also couldn't play it directly.In my PC, it also reminds me it has been damaged.
If you search it in the internet, .ts file is encrypted(In the page of your url,the way it encrypt is AES128).Maybe you need to take some measures.

Replace your code with the below code. I hope it will work :).
open('E:/video.mp4', 'wb').write(resp.content)

Related

Download File or Video from URL (Python 3)

ı tried diffrent libs to download video from url. But even one of them didnt worked.
Here is the link, that ı trying: https://td-cdn.pw/api.php?download=tikdown.org-42500282235.mp4
If it opened once, it directly asking to download, not like a html video. And ı want to save this video to local folder.
If you guys help me ı would be so proud :) (btw ım freaking ı try to solve it last 4 hours)
There are two steps to getting file downloaded in Python so the process is os independant. I would recommend using inbuilt requests library. We use it to make requests to server and fetch content. Then we write the data into a file in next step.
import requests
URL = "https://td-cdn.pw/api.php?download=tikdown.org-42500282235.mp4"
FILE_TO_SAVE_AS = "myvideo.mp4" # the name you want to save file as
resp = requests.get(URL) # making requests to server
with open(FILE_TO_SAVE_AS, "wb") as f: # opening a file handler to create new file
f.write(resp.content) # writing content to file
But this is just a simple example. You can implement other features like try/catch blocks to catch any exceptions or use custom headers while making requests.

python request.get response object to download xlsx file from url saves excel file but file is smaller and not accessible

Although I use stackoverflow regularly to solve problems, this is my first post :). I hope you can help!
I have a link that automatically downloads a .xlsx file. You get directed to a simple screen with a link if the download does not start automatically.
I can download the file and save it to disk.
However, the .xlsx file that is saved is smaller (2kb vs 6kb) and I cannot open it. When I open it with LibreOffice Calc it asks to select a language to use for import and then nothing happens.
Maybe some encoding/decoding problem?
I have tried several different methods to retrieve the file and save it to disk from various threads, but often with the same result.
This is my code:
resp = requests.get(url)
with open('filename.xlsx', 'wb') as output:
output.write(resp.content)

How can I download free videos from youtube or any other site with Python, without an external video downloader?

Problem
Hi! Most answers refer to using pytube when using Python. But the problem is that pytube doesn't work for many videos on youtube now. It's outdated, and I always get errors. I also want to be able to get other free videos from other sites that are not on youtube.
And I know there are free sites and paid programs that let you put in a url, and it'll download it for you. But I want to understand the process of what's happening.
The following code works for easy things. Obviously it can be improved, but let's keep it super simple...
import requests
good_url = 'https://www.w3schools.com/tags/movie.mp4'
bad_url = 'https://r2---sn-vgqsknes.googlevideo.com/videoplayback?expire=1585432044&ei=jHF_XoXwBI7PDv_xtsgN&ip=12.345.678.99&id=743bcee1c959e9cd&itag=244&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278%2C298%2C299%2C302%2C303&source=youtube&requiressl=yes&mh=1w&mm=31%2C26&mn=sn-vgqsknes%2Csn-ab5szn7z&ms=au%2Conr&mv=m&mvi=4&pl=23&pcm2=yes&initcwndbps=3728750&vprv=1&mime=video%2Fwebm&gir=yes&clen=22135843&dur=283.520&lmt=1584701992110857&mt=1585410393&fvip=5&keepalive=yes&beids=9466588&c=WEB&txp=5511222&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cpcm2%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=ADKhkGMwRgIhAI3WtBFTf4kklX4xl859U8yzqavSzu-2OEn8tvHPoqAWAiEAlSDPhPdb5y4xPxPoXJFCNKr-h2c4jxKU8sAaaxxa7ok%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=ABSNjpQwRQIhAJkFK4xhfLraysF13jSZpHCoklyhJrwLjNSCQ1v7IzeXAiBLpVpYf72Gp-dlvwTM2tYzMcVl4Axzm2ARd7fN1gPW-g%3D%3D&alr=yes&cpn=EvFJNwgO-zNQOWkz&cver=2.20200327.05.01&ir=1&rr=12&fexp=9466588&range=15036316-15227116&rn=14&rbuf=0'
r = requests.get(good_url, stream=True)
with open('my_video.mp4', 'wb') as file:
file.write(r.content)
This works. But when I want a youtube video (and I obviously can't use a regular youtube url because the document request is different from the video request)...
Steps taken
I'll check the network tab in the dev tools, and it's all filled with a bunch of xhr requests. The headers for them always have the very long url for the request, accept-ranges: bytes, and content-type: video/webm, or something similar for mp4, etc.
Then I'll copy the url for that xhr, change the file extension to the correct one, and run the program.
Result
Sometimes that downloads a small chunk of the video with no sound (few seconds long), and other times it will download a bigger chunk but with no image. But I want the entire video with sound.
Question
Can someone please help me understand how to do this, and explain what's happening, whether it's on another site or youtube??
Why does good_url work, but not bad_url??? I figured it might be a timeout thing, so I got that xhr, and immediately tested it from python, but still no luck.
A related question (don't worry about answering this one, unless required)...
Sometimes youtube has Blob urls in the html too, example: <video src='blob:https://www.youtube.com/f4484c06-48ed-4531-a6ee-6a3ae0291d26'>...
I've read various answers for what blobs are, and I'm not understanding it, because it looks to me like a blob url is doing an xhr to change a url in the DOM, as if it was trying to do the equivalent of an internal redirect on a webserver for a private file that would be served based on view/object level permissions. Is that what's happening? Cause I don't see the point, especially when these videos are free? The way I've done that, such as with lazy loading, is to have a data-src attribute with the correct value, and an onload event handler runs a function switching the data-src value to the src value.
you can try this video
https://www.youtube.com/watch?v=GAvr5_EtOnI
from this bad_url remove &range=3478828-4655264
try this code:
import requests
good_url = 'https://www.w3schools.com/tags/movie.mp4'
bad_url = 'https://r1---sn-gvcp5mp5u5-q5js.googlevideo.com/videoplayback?expire=1631119417&ei=2ZM4Yc33G8S8owPa1YiYDw&ip=42.0.7.242&id=o-AKG-sNstgjok92lJp_o4pF_iJ2MWD4skzEvFcTLl8LX8&itag=396&aitags=133,134,135,136,137,160,242,243,244,247,248,278,394,395,396,397,398,399&source=youtube&requiressl=yes&mh=gB&mm=31,29&mn=sn-gvcp5mp5u5-q5js,sn-i3belney&ms=au,rdu&mv=m&mvi=1&pl=24&initcwndbps=82500&vprv=1&mime=video/mp4&ns=mh_mFY1G7qq0apTltxepCQ8G&gir=yes&clen=7874090&dur=213.680&lmt=1600716258020131&mt=1631097418&fvip=1&keepalive=yes&fexp=24001373,24007246&beids=9466586&c=WEB&txp=5531432&n=Q3AfqZKoEoXUzw&sparams=expire,ei,ip,id,aitags,source,requiressl,vprv,mime,ns,gir,clen,dur,lmt&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIgYZMQz5Tc2kucxFsorprl-3e4mCxJ3lpX1pbX-HnFloACIQD-CuHGtUeWstPodprweaA4sUp8ZikyxySZp1m3zlItKg==&alr=yes&sig=AOq0QJ8wRQIhAJZg4q9vLal64LO6KAyWkpY1T8OTlJRd9wNXrgDpNOuQAiB77lqm4Ka9uz2CAgrPWMSu6ApTf5Zqaoy5emABYqCB_g==&cpn=5E-Sqvee9UG2ZaNQ&cver=2.20210907.00.00&rn=19&rbuf=90320'
r = requests.get(good_url, stream=True)
with open('my_video.mp4', 'wb') as file:
file.write(r.content)

Python urlfetch returning bad data

Note: This question has been edited to reflect new information, including the title which used to be 'How to store a PDF in Amazon S3 with Python Boto library'.
I'm trying to save a PDF file using urlfetch (if the url was put into a browser, it would prompt a 'save as' dialog), but there's some kind of encoding issue.
There are lots of unknown characters showing up in the urlfetch result, as in:
urlfetch.fetch(url).text
The result has chars like this: s�*��E����
Whereas the same content in the actual file looks like this: sÀ*ÿ<81>E®<80>Ùæ
So this is presumably some sort of encoding issue, but I'm not sure how to fix it. The version of urlfetch I'm using is 1.0
For what it's worth, the PDF I've been testing with is here: http://www.revenue.ie/en/tax/it/forms/med1.pdf
I switched to urllib instead of urlfetch e.g.
import urllib
result = urllib.urlopen(url)
...and everything seems to be fine.

What is a good audio library for validating files in Python?

I'm already checking for content-type, size, and extension (Django (audio) File Validation), but I need a library to read the file and confirm that it is in fact what I hope it is (mp3 and mp4 mostly).
I've been here: http://wiki.python.org/moin/Audio/ but no luck. Been at this one for a while, am a bit lost in the woods. Relying on SO big time for this whole end of things...
Thanks in advance.
EDIT:
I'm already (in Django) using UploadedFile.content_type() :
"The content-type header uploaded with the file (e.g. text/plain or application/pdf). Like any data supplied by the user, you shouldn't trust that the uploaded file is actually this type. You'll still need to validate that the file contains the content that the content-type header claims -- "trust but verify."
So, I'm already reading the header. But how can I validate the actual content of the file?
If just checking the header isn't good enough, I'd recommend using mutagen to load the file. It should throw an exception if it's not correct.
FYI, I do not think your approach is very scalable. Is it really necessary to read every byte of the file? What is your reason for not trusting the file header?
You can call a unix sub-shell within python like this:
>>> filename = 'Giant Steps.mp3'
>>> import os
>>> type = os.system('file %s' % filename)
Giant Steps.mp3: ISO Media, MPEG v4 system, iTunes AAC-LC
** See man pages for more details on the 'file' command if you want to go this route.
See this post for other options
Use sndhdr
It does a little more than content-type. Reads the file and gets it's headers..of course this is still not foolproof..using ffmpeg is probably then the only option.

Categories

Resources