Show Video with audio in VLC - python

i have an Python Program that show photos and videos from a folder. The files in this folder a changing so a static playlist is not possible. I can create a playlist with a media_list_player
I create an instance with
instance = vlc.Instance('--input-repeat=-1 --aout=alsa --alsa-audio-device=plughw:0,0')
mediaList = instance.media_list_new()
mediaList.add_media(instance.media_new(tmpPath + "/start.jpg"))
list_player = instance.media_list_player_new()
list_player.set_media_list(mediaList)
list_player.audio_set_volume(100)
list_player.play()
And i add new files with
mediaList.add_media(instance.media_new(showFile))
list_player.set_media_list(mediaList)
This works but when there is an video i dont have sound. "audio_set_volume()" dosen work. I tried the normal media_player but there i cant make a playlist. So i hope someone here can help me with adding sound to the vlc.

Related

Add audio to video using moviepy

Title basically explains it. Trying to add audio to a video. I have a sequence of mp3 files that I want to play sequentially during the video. There're no errors or anything with the code but when I run the code theres still no audio in the video. Just a recreation of the old video with no audio (the original video doesnt have audio either).
video = VideoFileClip("finished_video.mp4")
title_clip = AudioFileClip("title.mp3")
audio_list = [title_clip]
for x in range(1, counter):
audio = AudioFileClip("p{}.mp3".format(x))
audio_list.append(audio)
video.set_audio(audio_list)
video.write_videofile("new_filename.mp4")
it works for me
from moviepy.editor import *
videoclip = VideoFileClip("sample.mp4")
audioclip = AudioFileClip("sample_audio.mp3")
new_audioclip = CompositeAudioClip([audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("output.mp4")
use CompositeAudioClip to merge both files.
I had the same issue. It seems like set_audio() doesn't actually set it, but returns the new video combined with the audio.
This is the way I did it :
final_video = video.set_audio(audio_list)
final_video.write_videofile("new_filename.mp4")
Hope that helps !

How do I display a PDF page in pygame without having to save a file in a folder in between?

I have created an app that runs with PyGame, which loads a page of a PDF, creates a Pixmap with getPixmap and saves into a jpg file. Then, the image is displayed with pygame.image.load().
The program works perfectly fine, but it requires to save a .jpg file in the folder, which substitutes and therefore deletes the previous .jpg file. When converting it into an exe and distributing the program, the users get a security message error, saying that the program does not have the permission to delete the .jpg file.
How could I skip the part of saving the photo in the folder, and directly showing from the PDF into pygame?
I think it can be done by creating a file-like object with Bitmap.IO(), but I don't know how to use it. I'm relatively new in python, so I would appreciate if you could write the lines of code you would use.
zoom = 2
doc = fitz.open(pdf_path)
mat = fitz.Matrix(zoom, zoom)
page = doc.loadPage(photo_num)
pix = page.getPixmap(matrix = mat)
photo_output = "photo.jpg"
pix.writePNG(photo_output)
load_photo = pygame.image.load(photo_output)

moviepy ruining video after combining them

I'm trying to make a program that downloads videos from TikTok and combines all the separate videos into one .mp4 file and moves the final video to a folder on my desktop. I've been able to make it download all the videos and when I watch the separate videos they play fine however when I combine the videos some of the videos are messed up and look like this but the audio is fine.
#slecting all .mp4 files
video_files = glob.iglob("*.mp4")
print(video_files)
clips = []
for clip in video_files: # For each mp4 file name
clips.append(VideoFileClip(clip)) # Store them as a VideoFileClip and add to the clips list
today = date.today()
final = concatenate_videoclips(clips) # Concatenate the VideoFileClips
final.write_videofile(f"{today}.mp4", codec="libx264")
#moving completed video to folder on desktop
shutil.move(f'{today}.mp4', '/Users/jacobmarrandio/Desktop/done_videos/')
thanks for any help
You want to ensure that all of your videos are of the same size before concatenating. Or, you could modify the smaller clips to have a black margin by filling up space by making this change:
final = concatenate_videoclips(clips, method='compose')

Choose where to save the image when you call save_frame() in MoviePy

I'm using MoviePy to save a frame from an uploaded video (the specific function in MoviePy is save_frame() source). This works successfully, however I want the image to save in my media folder (Django's conventional folder for saving uploaded files). At the moment it saves in the project root (where manage.py is). There doesn't seem to be a settings file so I'm not sure how to change this. Any suggestions appreciated.
This is the main idea to achieve that, maybe you have to make little adjustments:
# Your target directory
destination_dir = os.path.join(settings.BASE_DIR, 'site_media', 'media', 'video_thumbs')
# Full path to file to open, I took the file path from filefield
origin_file = os.path.join(settings.MEDIA_ROOT, model.filefield.path)
# Get thumbnail
clip = VideoFileClip(origin_file)
# Save thumbnail on target directory
clip.save_frame(os.path.join(destination_dir, "thumbnail.jpg"), t=1.00)

Record RTSP stream to file (.wav)

I'm trying to save X seconds from a audio stream to a file. I have a RTSP server, and I made a simple script in python to save several seconds from this server to record in a file (.wav).
def main():
########################### MAIN INIT ###########################
instance = vlc.Instance("-vvv", "--no-video", "--clock-jitter=0", "--sout-audio", "--sout",
"#transcode{acodec=s16l,channels=2}:std{access=file,mux=wav,dst=test.wav}")
# Create a MediaPlayer with the default instance
player = instance.media_player_new()
# Load the media file
media = instance.media_new("rtsp://XXX.XX.XXX.XX:YYYY/")
# Add the media to the player
player.set_media(media)
# Play for 10 seconds then exit
player.play()
time.sleep(10)
if __name__ == '__main__':
main()
But when I run the script it creates the file "test.wav" but it's a text plane file instead of wav, what it's I'm waiting for.
Log show me next info:
[00000000022aec08] core input error: ES_OUT_RESET_PCR called
[00007f6704040518] core decoder error: cannot continue streaming due to errors
So I really appreciate someone who can help me.
Thank so much.
Wav files are structured with different fields representing different information as you probably know - see an exmample here from this link (https://github.com/kushalpandya/WavStagno):
It sounds like your output is not formatted correctly - there are tools available to inspect a WAV file which would be a good place to start, or if you are bale to share a link to the file here then people can take a look.
If what you are trying to do is to listen to the stream and save it at the same time, then you likely want to use the duplicate functionality - there is a god example here (albeit video based): https://stackoverflow.com/a/16758988/334402

Categories