[PYTHON] Share VLC playlists on PC and Android

Overview

I wanted to use the same playlist for music files on the NAS on both PC and Android.

This time, create it with "xspf" of the playlist file with VLC.

Even if I create a playlist on my PC and use it on Android as it is, I get an error saying "The location file: /// cannot be played" and cannot use it.

As a result of various investigations, if the file path of the playlist is set to ** relative path **, it can be opened on both PC and Android.

Playlist creation procedure

  1. Create a folder to save the playlist At this time, if you make the target file as close as possible to the location, it will be easier to think about the relative path when rewriting the file later. プレイリスト作成説明図1.png

  2. Load the video file into VLC on your PC by dropping and dragging

  3. Save the playlist

  4. Open the saved playlist and rewrite it to a relative path Since there is a lot of rewriting here, I will do it with a script etc.

  5. Will be available on PC and Android

  6. Very convenient

Python to rewrite to the relative path used in step 3

プレイリスト作成説明図2.png When I open "xspf" created on the PC, the file path is written as "absolute path", so I decided to use Python to rewrite it as "relative path".

The path before rewriting in "xspf" is `ʻE: // music / album 01 / music.mp3, and if you make this a relative path at the above folder location, ../ album 01 / It becomes music.mp3``.

This time, the file path is `ʻE: // ``, but if you rewrite it for the same with ** shared IP address **, you can use it as it is.

So, create the following Python in ** Playlist folder ** and execute it to rewrite it to a relative path.

import shutil
import os
from pathlib import Path

def replace(file_path):
    file_name = file_path
    #Creating a backup file
    print('path: ', file_path)
    back_name = str(file_name) + '.bak'
    print('back_name: ', back_name)
    shutil.copy(file_name, back_name)

    with open(file_name , encoding=`utf-8`) as f:
        data_lines = f.read()


    #String replacement
    # 「["When"]""%5B"When"%If you do not replace it with "5D", "vlc":It becomes "nop" and does not load
    data_lines = data_lines.replace('file:///E://music/', '../').replace('[', '%5B').replace(']', '%5D')

    #Save with the same file name
    with open(file_name, `w`, encoding=`utf-8`) as f:
        f.write(data_lines)

#Get the folder where your py is
p_temp = os.path.dirname(__file__)
#Get a list of xspf files in a folder
for f in Path(p_temp).glob(`**/*.xspf`):
    replace(f)

Recommended Posts

Share VLC playlists on PC and Android
Create and run Discord Bot on one Android device
Interact with Python on Android from PC via adb
How to share OS and Vim clipboard on Ubuntu 18.04.3 LTS
USB device programming with native C on Android 5.0 and above