1

I am trying to mix two .mp3 songs together by simply changing the volumes of two songs, similar to DJing. However, when I set the volume of a player, both players' volumes are being changed to the value that I've last set. I would like to create two separate players that have different volume properties AKA for example to have one player with volume(100) and the other set to volume(20). Here is how I am doing it:

import vlc
import time

# Path for mp3 file
song = 'C:/Users/Admin/Desktop/Projects/Music Shit/Martin Garrix - Animals (Original Mix).mp3'

# Set up and play player with volume 100
player = vlc.MediaPlayer(song)
media = vlc.Media(song)
player.set_media(media)
player.audio_set_volume(100)
player.play()

# Path for second mp3 file
song2 = 'C:/Users/Admin/Desktop/Projects/Music Shit/Tremor (Sensation 2014 Anthem).mp3'

# Set up and play second player with volume 20
player2 = vlc.MediaPlayer(song2)
media2 = vlc.Media(song2)
player2.set_media(media2)
player2.audio_set_volume(20)
player2.play()

When I run this, both songs play at volume 20, which is undesired. I believe that they are linked to one player, which I do not want. I'd like to have two separate players with different volumes.

By the way when I tried this on Mac it worked, and the audio had different volumes, but I am currently on Windows and it is not working. Seems weird!

Any help would be much appreciated. It's my first time submitting a question!

2 Answers2

0

I found this problem really interesting, so I downloaded the vlc python module and tinkered with it, and I think I found a solution for you.

What I did is I built an array containing the VLC instances, I built a function that creates VLC instances, and a loop that makes sure they're running (and produces verbose output).

This does require you make sure that your VLC player allows multiple window instances.

My code:

import vlc
import time

VLCObjects = []

def VLCInstance(src, volume):
    
    # creating vlc media player object 
    try:
        vlc_instance = vlc.Instance() 
        player = vlc_instance.media_player_new() 
        media = vlc_instance.media_new(src)
        player.set_media(media)
    except:
        return("Error: Was unable to mount media")
    else:
        pass
    
    VLCObjects.append(player)

    VLCObjects[0].audio_set_volume(volume) 
    VLCObjects[0].play() 

    # give time to initialize, then get length of audio & wait until done playing
    time.sleep(1) 
    duration = VLCObjects[0].get_length() 
    #time.sleep(duration)
    return("Success: Song playing")

Then you'd make all your calls or whatever here, and then the loop:

song1 = ...
song2 = ...
VLCInstance(song1, 100)
VLCInstance(song2, 20)
while(True):

    playing = False
    
    time.sleep(1) 
    
    print(VLCObjects)
    for x in range(0, len(VLCObjects) ):
        value = VLCObjects[x].is_playing()

        if value == True:
            print("... VLCObject #%d is playing" % x)
            playing = True
            
        else:
            print("... VLCObject #%d needed to be restarted" % x)
            VLCObjects[x].play()
            
    if playing == False:
        break

The resulting output will look something like this, and there will be two non-windowed VLC players.

Success: Song playing
Success: Song playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
0
0
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
JohnAlexINL
  • 615
  • 7
  • 15
  • 1
    Unfortunately this did not do it for me. When I call song2 with volume(20), both tracks have set their volume to 20. I am on windows 7 with Python 3.8.5. It's weird that it worked fine for me on a Mac. Do you know what the issue is here? – yellowazns123 Dec 30 '20 at 19:48
  • I'm not sure what the issue you're having is, but I would try adding a second `if, else` block into the `while` loop to check if the volume is what was originally called for. You'd just need another array, maybe `VLCVolumes` at the beginning, and for the value to be saved when you call the `VLCInstance` function. I hope that fixes the problem for you! – JohnAlexINL Dec 31 '20 at 00:56
  • I'm running Linux Mint 20 (Cinnamon 4.6.6) on a Samsung laptop, also running 3.8.5 – JohnAlexINL Dec 31 '20 at 01:00
0

A variation on a theme, using multiple instances of vlc.Instance is the way to go.

import vlc
import time

files = ['./V2.mp4','./vp1.mp3','./V3.mp4'] 
instances = []
medias = []
players = []


for idx, fname in enumerate(files):
    print("Loading",fname)
    instances.append(vlc.Instance())
    medias.append(instances[idx].media_new(fname))
    time.sleep(1) # used solely to gauge the difference in volumes
    players.append(vlc.MediaPlayer())
    players[idx].set_media(medias[idx])
    players[idx].play() 

players[1].audio_set_volume(120)
players[2].audio_set_volume(80)
player_count = players # copy of the players list so we don't modify during iteration
still_playing = True
time.sleep(0.5) # Wait for players to start

while still_playing:
    time.sleep(1)
    for p in players:
        if p.is_playing():
            continue
        else:
            player_count.remove(p)
            players = player_count # no point iterating over players that have finished
            print("Finished - Still playing ", str(len(player_count)))

    if len(player_count) != 0:
        continue
    else:
        still_playing = False
        
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60