LibVLC - Disabled popup video player - python

I'm trying to use libVLC in a Python program to play an online live stream.
The program passes .m3u8 link to libVLC, which works fine.
When the stream starts, it opens a window titled VLC (Direct3D11 output).
However, I only want the audio to play, not the video to show. This is the usual behaviour with a .mp3 file for example.
In short: how can I disable the video output of libVLC, to have it play just the audio of a live stream, with no window?

I don't know libVLC very well, but I had a similar problem with the python-vlc module.
In my case, I wanted to play the audio of a youtube video/stream.
I modified the code from here by adding the --no-video argument when instantiating the vlc object.
This is basically a general VLC command line argument to suppress video output.
Maybe there is a similar option to add this in libVLC?
Below the code, just for reference:
import pafy
import vlc
url = "YOURVIDEOURL"
video = pafy.new(url)
best = video.getbest()
playurl = best.url
Instance = vlc.Instance("--no-video") #This did the trick
player = Instance.media_player_new()
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()
input()

Related

Save video via python on windows logoff

I am using this code to capture everything on my monitor and save it to mp4 file:
# screen recording config
sct = mss()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(recording_file_name_location, fourcc, 20.0, (1920, 1080))
# while loop that captures the while screen image by image
while True:
sct_img = sct.grab(screen_resolution)
out.write(np.array(sct_img))
The problem is I want this code to stop recording once I logoff from my Windows machine, that does happen, and the video is saved on my Desktop, but it is not playable.
If I run this code via PyCharm and I end the code via PyCharm (CTRL + C or I use PyCharm stop button), then it does save the mp4 file and I can play it without any problems.
Also, if I start the code from the CMD it works fine, but if I close CMD windows on the "X" in the top right corner, video is again unplayable.
I guess that this happens due to killing Python process in the background, so the script/code never saves the file properly.
Do you guys have any suggestion for this problem? How to save the mp4 video once I logoff or turn off my Windows machine?
Thank you in advance
The process was killed abruptly.
The video is not playable because out.release() could not be called. Tthe video writer was not given the chance to write index structures into the video file, which can only be done when the file is complete.
Simple fix: make a .ts file instead of any other format/extension. These "transport stream" formats never need finalizing.
Perhaps you need to learn about how Windows ends processes when you log off. Usually Windows sends each application/window a WM_QUERYENDSESSION and WM_ENDSESSION message, telling it to end itself before it gets ended by force.

Python play audio in sync with cv2 video

I am very new to the cv2 library, and since I recently discovered it is unable to play audio I wanted to know if there is another library that will allow me to play the audio in sync with the video itself (preferably without needing an audio file, just using the video's audio)
Turns out there is a very simple method that I will found after researching later, unfortunately, it requires a separate audio file that matches the video:
First, import simpleaudio as sa (install it with pip if necessary) and then add this piece of code to your cv2 video player. Add it before the while loop but after the line of code that defines the video file:
wave_obj = sa.WaveObject.from_wave_file("AudioFile.wav")
wave_obj.play()
Then you must manually adjust the waitKey until the audio matches the video, otherwise it might be too fast or too slow. Generally a value close to 25 must be used, and if the audio abruptly ends, try adding one to the value until it matches

Why won't a .mpg video load into pygame's movie module

I've been trying to get a short video to load and play within a pygame window. However, the program stalls when it tries to load the video. I'm also unable to abort the debugger.
import pygame
import pygame.movie
pygame.init()
print('TEST')
video = pygame.movie.Movie('D:\\Presentation\\video.mpg')
print('TEST')
This outputs 'TEST' once, so the video has not loaded (It keeps running "pygame.movie.Movie('D:\Presentation\video.mpg')" indefinitely).
I let it run for an hour but no luck, it keeps loading without any kind of exception or visible progress and the video is 1,636 KB large, 4 seconds long and was converted from a .mp4 using ffmpeg.
The directory is certainly correct for I tested with images in the same directive. I also restarted my PC (Windows 8.1 64-bit Python 3.2.5.1).
EDIT: I need an answer that will be relevant to the pygame module
I suggest you use Pyglet and follow the code I create here that plays a video.

How to close program in python opened by os.system()?

In python 3.4 , I was trying to open a "wav" file using vlc in Linux. Here is my code:
import os,time
os.system("cvlc audio/some.wav")
time.sleep(3) #audio was one and half sec
a = 3+3
print (a)
It plays the audio but then doesn’t do the rest. What should I do to make it do them? more precisely what should I do to close the vlc program?
With solving the problem it will also be very grateful to know is there any easier way to play audio within the code specifically in python 3.4?
(platform independent code will be even more grateful!)
So the VLC player doesn't exit. The VLC player has a command line argument to close the player once the song/video has been played.
Playlist
These options define the behavior of the playlist. Some of them can be overridden in the playlist dialog box.
--play-and-exit, --no-play-and-exit
Play and exit (default disabled)
Source: https://wiki.videolan.org/VLC_command-line_help
Can you try the following?
os.system("cvlc audio/some.wav --play-and-exit")

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