Tweet mp4 files with tweepy - python

Hey I'd like to tweet a mp4 file generated using ffmpeg to twitter.
However it seems that there's no real native way to do this with the official tweepy.
When tried with the api.update_with_media(filename, message) method, you'll get an error, unsupported file 'video/mp4'
So first I saw a discussion where this github project was linked: Github link
But after testing out the code, the app crashs after the first INIT phase.
So next I saw this: Github pull request thread
And on that thread it was announced that there is a code to tweet video but it hasn't officialy been merged yet, and braian87b made a fork of tweepy with the code to upload videos.
However I have no idea how to install and use a fork of tweepy and how to actually use the code to tweet a video. Braian87b used this as an example code:
upload_result = api.media_upload('/home/user/video.mp4')
api.update_status(status="test tweet", media_ids=[upload_result.media_id_string])
Any help would be apreciated! :)

In the end I managed to understand it. Making a fork of my own (as the one braian87b provided was incompatible with the latest version of pip, which is what I used to install it), I was able to install the forked tweepy by using pip
pip install git+https://github.com/Spyder-exe/tweepy.git
and then the code that braian87b provided was in the end perectly good!
So using this code
upload_result = api.media_upload('/home/user/video.mp4')
api.update_status(status="test tweet", media_ids=[upload_result.media_id_string])
And making sure to put the reply_to_status_id tag before the media_ids tag, I was able to upload videos to twitter with only minor annoyances.

I know this is late, but if you want to keep using the official tweepy, you can use the imgur API to upload your media, and then tweet the link (this is what I ended up doing)
update_with_media has been deprecated for a long time and once you set up the authentication with the imgur API it's really easy.

we are sending MP4 with less duration but getting media processing status is failed. so what is the format?
ANS
Twitter currently supports the following video formats: MOV and MP4 formats for mobile apps. The minimum Twitter video length is 0.5 second and the maximum Twitter video length is 2 minutes 20 seconds (140 seconds) Twitter supports the following web video formats: MP4 video uploads with H264 format and AAC audio
Use mobile record video you will get your answer

Related

Can I tweet mp4 files with tweepy?

I was just wondering if it was possible to tweet an mp4 file with tweepy (maybe using api.update_with_media). I've seen a post from a few years ago saying that it's not possible with the official tweepy, but I find it really hard to believe that there's no way to do it with the official tweepy. Can anyone please help?
update_with_media was deprecated as a Twitter API method about 5 years ago. This pre-dated the ability to upload GIFs and video MP4s. If you want to post media on Twitter via the API, you must find a library that supports the media upload endpoints (and chunked uploads). The process is
upload the media file (chunked if necessary depending on size and format)
retrieve a media ID string
post a Tweet, and add the media ID to the Tweet.
Note that you can only post multiple images on a single Tweet; you can only post a single GIF or video file on a single Tweet, not multiple.
I do not believe the default Tweepy release supports this but I could be wrong, you’ll need to check the Tweepy documentation.
Do NOT use update_with_media - it’s very old and unsupported as an API path.
Also worth being aware that tweepy itself is a third party library - that we at Twitter LOVE - but this is not officially supported.

Downloading past twitch broadcasts in python

I have been trying to download past broadcasts for a streamer on twitch using python. I found this python code online:
https://gist.github.com/baderj/8340312
However, when I try to call the functions I am getting errors giving me a status 400 message.
Unsure if this is the code I want to download the video (as an mp4) or how to use it properly.
And by video I mean something like this as an example: www(dot)twitch.tv/imaqtpie/v/108909385 //note cant put more than 3 links since I don't have 10 reputation
Any tips on how i should go about doing this?
Here's an example of running it in cmd:
python twitch_past_broadcast_downloader.py 108909385
After running it, it gave me this:
Exception API returned 400
This is where i got the information on running it:
https://www.johannesbader.ch/2014/01/find-video-url-of-twitch-tv-live-streams-or-past-broadcasts/
Huh it's not as easy at it seems ... The code you found on this gist is quite old and Twitch has completely changed its API. Now you will need a Client ID to download videos, in order to limit the amount of video you're downloading.
If you want to correct this gist, here are simple steps you can do :
Register an application : Everything is explained here ! Register you app and keep closely your client id.
Change API route : It is no longer '{base}/api/videos/a{id_}' but {base}/kraken/videos/{id_} (not sure about the last one). You will need to change it inside the python code. The doc is here.
Add the client id to the url : As said in the doc, you need to give a header to the request you make, so add a Client-ID: <client_id> header in the request.
And now I think you will need to start debugging a bit, because it is old code :/
I will try myself to do it and I will edit this answer when I'm finished, but try yourself :)
See ya !
EDIT : Mhhh ... It doesn't seem to be possible anyway to download a video with the API :/ I was thinking only links to API changed, but the chunks section of the response from the video url disappeared and Twitch is no longer giving access to raw videos :/
Really sorry I told you to do that, even with the API I think is no longer possible :/
You can download past broadcasts of Twitch videos with Python library streamlink.
You will need OAuth token which you can generate with the command
streamlink --twitch-oauth-authenticate
Download VODs with:
streamlink --twitch-oauth-token <your-oauth-token> https://www.twitch.tv/videos/<VideoID> best -o <your-output-folder>

Accessing youtube video stream map (python)

I'm trying to write a python script that will download a youtube video, using this line of code for getting the download url:
download_url = "http://www.youtube.com/get_video?video_id={0}&t={1}&fmt=22&asv=2".format(video_id, token_value)
(video_id and token_values being info I've got from parsing youtube video url)
but this keeps downloading empty file.
Since this method is old, is there now some other form of getting download url for youtube videos?
I solved my problem (to some extent) in the meantime. I just had to access youtube video stream map and grab one of the URLs stored there and download the file. However, this works only on videos that don't have their signature encrypted. I'm still working on a solution for videos with encrypted signatures.

Use tweepy to get Twitter videos from user timeline

When using tweepy to collect video tweets, generally the Status object returned has an extended_entities attribute that contains media information like the direct link to the mp4 file.
It appears however that statuses with Amplify videos (amp.twimg.com) are missing this extended_entities attribute and so I am having trouble collecting the media url. They have a link to the video that is fine if you are navigating with a browser (http://amp.twimg.com/v/50bac95c-1508-40c6-a0fc-c1b26a53a3b8 for example) but this is not very useful if I just want the mp4 file.
Is there a way to collect the mp4 file from Amplify videos using tweepy? Why do these videos not have the same media information as other twitter videos?
As you can see in the official documentation, you have to opt into the extended mode via a request parameter:
"Any endpoints that return Tweets will accept a new tweet_mode request parameter. Valid request values are compat and extended, which give compatibility mode and extended mode, respectively."
So maybe you'll have better results if you explicitly pass tweet_mode='extended'. Not sure if this has been implemented in Tweepy yet. Just check the sourcecode.

Python Youtube Audio Downloader using Website

So I am writing a simple script to extract links from a Youtube video, and then download the audio from each of the links. Extracting the links I got down using urllib, however, youtube_dl is being more problematic.
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(["https://www.youtube.com/watch?v=YQHsXMglC9A"])
I have the correct "ydl_opts" and everything, but it keeps rasing:
DownloadError: ERROR: ffprobe or avprobe not found. Please install one.
Even after Googling this, and installing ffprobe, I keep getting this error... and the weird thing is I can import ffprobe at the beginning of the program but youtube_dl doesn't detect it(?).
So I am trying another idea:
Use a third party website like "youtube-mp3.org", get a template link like:
"""http://www.youtube-mp3.org/get?video_id={1}&ts_create=1451072960&r=MjQuMjEuMTc5LjU2&h2=12c386ccffa12ee7b16dd25078df2558&s=156489""".format(video_link_id)
... then formatting it with Youtubes unique video ID, and download it with urllib.urlopen(), but that download link generates unique arguments that can't be used with other videos. So is there a way to download from a website like that, and/or supply argument like changing the download URL algorithm? Or something to that effect?
Overall objective:
Get Youtube video ID
Download and convert to mp3 or similar format
Play audio within Python
Bonus:
Do it as efficiently and quickly as possible (i.e Not downloading entire video, or extra conversion steps)
I can recommend you a package called pytube.
I have used it and am very pleased with the results. It downloads the video, and after that you can use a library to extract only the audio.
Hope it helps.

Categories