How to capture network stream as image in Python? - python

In VLC I can do "Media -> Open Network Stream" and under "Please enter a network URL" I can put udp://#239.129.8.16:1234.
And this is opening local UDP video stream.
VLC is not related to my question, I have put it just for clarification.
How can I connect to "udp://#239.129.8.16:1234" network stream in Python, get image from it (screenshot) and save it in file?

I think neither network programming nor Python is the focus of your question here. At the core, you need to feed a video decoder with the binary data stream, make the video decoder collect a sufficient amount of data for decoding a single frame, let the decoder save this, and abort the operation.
I am quite sure that the command line tool avconv from the libav project can do everything you need. All you need to do is dig into the rather complex documentation and find the right command line parameters for your application scenario. From a quick glance, it looks you will need for instance
‘-vframes number (output)’
Set the number of video frames to record. This is an alias for -frames:v.
Also, you should definitely search the docs for this sentence:
If you want to extract just a limited number of frames, you can use
the above command in combination with the -vframes or -t option
It also looks like avconv can directly read from a UDP stream.
Note that VLC, the example you gave, uses libav at the core. Also note that if you really need to execute all this "from Python", then spawn avconv from within Python using the subprocess module.

Related

How to stream a video with youtube-dl in Python 3

I would like to stream the audio of a youtube video in python, youtube-dl allows me to download a video (audio in my case), but that process can take some time. My objective is to be able to stream the audio 'dynamically', like I would by going on a youtube video. I would like to start playing the audio and still download the rest of it at the same time.
I know that the youtube-dl command line program allows to stream videos to media players, like VLC for example:
youtube-dl -o - -- "[videoID]" | vlc -. I could create a subprocess and execute that command, but I would prefer to use a cleaner way, if possible.
I would expect to have some sort of data that I can transmit to an audio device later on. I don't need to store the audio in a file, but it's not a big deal if there is a temporary file.
This is unfortunately not possible. Youtube-DL does not expose an API that makes this straightforward. This is where Youtube-DL opens the file (or stdout) for writing. It's not exactly written to allow for easy switching of the output stream.
It's probably easier to just subprocess it and pipe its output if you really want this functionality.

How to use python with qr scanner devices?

I want to create a program that can read and store the data from a qr scanning device but i don't know how to get the input from the barcode scanner as an image or save it in a variable to read it after with openCV
Typically a barcode scanner automatically outputs to the screen, just like a keyboard (except really quickly), and there is an end of line character at the end (like and enter).
Using a python script all you need to do is start the script, connect a scanner, scan something, and get the input (STDIN) of the script. If you built a script that was just always receiving input and storing or processing them, you could do whatever you please with the data.
A QR code is read in the same way that a barcode scanner works, immediately outputting the encoded data as text. Just collect this using the STDIN of a python script and you're good to go!

Is there a way to live stream audio from server without the local file?

I'm looking for a way to continuously stream audio from a server, the main issue is that the server side code it will receive many url's to stream audio. There will also be instances where the url is swapped live and a new piece of audio is streamed instead. I have not yet found a solution that wouldn't involve me downloading each file to then stream, which would hinder the live feature.
I've attempted to use vlc for python but it wouldn't allow for the ability to change the url being streamed in the moment. I've also attempted to use pyaudio but I haven't been able to get the correct audio format let alone swap the source of the audio.
An example link, fairwarning it'll autoplay: audio
To make a continuous stream that is sent to clients, you'll need to break this project into two halves.
Playout
You need something to decode the source streams from their compressed formats to a non-compressed standardized format that you can manipulate... raw PCM samples. Use a child process and have it output to STDOUT so you can get that data in your Python script. You can use VLC for this if you want, but FFmpeg is pretty easy:
ffmpeg -i "http://example.com/stream" -ar 48000 -ac 2 -f f32le -acodec pcm_f32le -
That will output raw PCM to STDOUT as 32-bit floats, in stereo, at 48 kHz. Once in this standard format, you can arbitrarily join streams. So, when you're done playing one stream, just kill the process, switch to the next, and start playing back samples from the new one.
Encoding
You want to create a single PCM stream that then you can re-encode with some external encoder, basically in reverse from what you did on playout. Again, something FFmpeg can do for you:
ffmpeg -f f32le -ar 48000 -ac 2 - -f opus -acodec libopus icecast://...
Now, you'll note the output example here, I suggested sending this off to Icecast. Icecast is a decent streaming server you can use. If you'd rather just output directly over HTTP, you can. But if you're playing this stream out to more than one listener, I'd suggest letting Icecast or similar take care of it for you.

Playing an in-memory stream with LibVLC

I'm trying to add HLS playback to a Python application. Currently, the most appropriate path to go down seems to be to use LibVLC and its Python bindings, as its the only multimedia library I've found for Python which can play MPEG transport streams out of the box (or at all). I'm open to other suggestions, though.
However, I also need my application to handle the fetching of the MPEG TS chunks from the HLS manifest itself, in order to set an appropriate user agent, manage proxy settings and store cookies between HTTP requests. Therefore, I have a thread downloading HLS chunks and adding them to a queue, which then feeds them into a BytesIO instance. I can easily enough save that instance to disk to emulate download functionality, but my question is, how can I feed the data from a BytesIO stream into LibVLC in order to play the stream in realtime?
I've tried using ctypes along with libvlc_media_new_callbacks (see my previous question here), but didn't get very far. I've also tried passing file descriptors of temporary files or pipes created using os.pipe, but VLC doesn't seem to be able to inherit and access these. If I save each chunk to its own temporary file and then queue them up in VLC, there is a gap in playback between each one. So I'm a bit stuck.
Any help would be very much appreciated!

Stream record program VLC

I am trying to record a live stream in vlc. It is easy if I use the GUI, just clicking on Convert/Save in the Media option, and after that choosing the stream address in the Network tab. I wanted to do the same thing in a C/C++/Python program. In case of a C program, I used Visual Studio but on writing #include<vlc/vlc.h> it says the file cannot be included. Then I downloaded the source from git but still it is not working. What to do?
You can save a stream using commandline arguments:
vlc scheme://host/stream.xyz --sout file/muxer:stream.xyz
and thus, call it using some kind of exec() (or its windows equivalent).
Then, the following answer: https://stackoverflow.com/a/19484168/1290438 shows how to open a stream in VLC in python:
import vlc
i = vlc.Instance('--verbose 2'.split())
p = i.media_player_new()
p.set_mrl('rtp://#224.1.1.1')
p.play()
So I guess, at worst, you can give the --sout argument to vlc.Instance, or at best there's a method on the instance to set up stream output.
In my humble opinion, using C/C++ for such a simple task is like killing a fly using a bazooka…

Categories