Creation of playlist to fake-split a full-album video I created this snippet to "split" my full-album videos into its individual songs without the need of actually splitting the video file.

I personally created this snippet because I enjoy reading all musics from a directory in shuffle mode and stumbling upon a full album kinda ruins the point.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

__author__ = 'JeromeJ'
__website__ = 'http://www.olissea.com/'

# Match example for this regex:
#   32. Random song title - 1:33:05
DEFAULT_PATTERN = '^[0-9]+\.\s*(?P&lt;SONG_TITLE&gt;.*?)\s*-\s*(?P&lt;START_TIME&gt;[0-9]+\:[0-9]+(?:\:[0-9]+)?)'

SONG_TITLE = '{ALBUM_TITLE}: {SONG_TITLE}'  # What the song will be titled
ALBUM_TITLE = ''

import pathlib
import re

import pdb

print('M3U8 PLAYLIST MAKER')
print('Please put a .txt file containing the songs\' list next to the album')
print('with the same name as the album file.')
print()
print('The playlist will be saved in current directory.')
print('The links in the playlist are relative, not absolute.')
print()

# TODO: Idea: Allow to drag-and-drop the file on the script to run it.
#   -&gt; Doesn't seem to work (Tried on W7 only so far)
filename = input('Path to album? ')

txt_file = str(pathlib.Path(filename).with_suffix('.txt'))

# Saves the playlist in the current directory with the same name as the album
playlist_file = pathlib.Path(filename).stem + '.m3u8'

pattern = input('Pattern for the songs\' list? (Default: {})\n'.format(DEFAULT_PATTERN)) or DEFAULT_PATTERN
song_title = input('Song title? (Default: {}) '.format(SONG_TITLE)) or SONG_TITLE
album_title = input('Album title? (Default: {}) '.format(ALBUM_TITLE)) or ALBUM_TITLE

playlist_entries = []

with open(txt_file, mode='r', encoding='utf-8') as song_list:
    for i, line in enumerate(song_list):
        r = re.search(pattern, line)

        # pdb.set_trace()

        start_time = r.group('START_TIME').split(':')
        start_time = \
            int(start_time[-1]) \
            + int(start_time[-2])*60 \
            + (int(start_time[-3])*60*60 if len(start_time) &gt; 2 else 0)

        playlist_entries.append(
            """#EXTVLCOPT:start-time={start_time}
#EXTVLCOPT:stop-time={stop_time}
#EXTINF:0, - {album_title}: {song_title}
{filename}""".format(
                start_time=start_time,
                stop_time='{stop_time}',  # To be set by the next iteration
                album_title=album_title,
                song_title=r.group('SONG_TITLE'),
                filename=filename
            )
        )

        if i:
            playlist_entries[i-1] = playlist_entries[i-1].format(stop_time=start_time)

# Up until the end of the file
playlist_entries[-1] = playlist_entries[-1].format(stop_time=0)

with open(playlist_file, 'w', encoding='utf-8') as f:
    f.write('\n\n'.join(playlist_entries))

print()
input('Done. Press Enter to leave.')


Any advice/opinion about how to improve this code in its Python-ness is welcome!

What 'could' be improved?
What is wrong or advised against?

EDIT: As I've been suggested I could/should turn this into a lib. For my excuse this is "just a snippet".

Also I want to keep it as KISS as possible.

Example:
The script turns this:

01. Once Upon a Time - 00:00
02. Start Menu - 01:28
03. Your Best Friend - 02:00


Into this:

#EXTVLCOPT:start-time=0
#EXTVLCOPT:stop-time=88
#EXTINF:0, - Undertale: Once Upon a Time
..\Full albums\Undertale OST.mp4

#EXTVLCOPT:start-time=88
#EXTVLCOPT:stop-time=120
#EXTINF:0, - Undertale: Start Menu
..\Full albums\Undertale OST.mp4

#EXTVLCOPT:start-time=120
#EXTVLCOPT:stop-time=144
#EXTINF:0, - Undertale: Your Best Friend
..\Full albums\Undertale OST.mp4
Instead of using print() you can add a newline character \n at the end of the previous printed string:

print('with the same name as the album file.\n')
print('The playlist will be saved in current directory.')


When you ask for a filename, you don't do any input validation. What if they enter an empty string? What if their path isn't real? I'm not familar with pathlib, and also not sure what you'd want to do. You could create an infinite loop that will only break when the user enters a valid/existing path. Alternatively you could end the program if an invalid path appears. You definitely shouldn't ignore errors or leave it to Python to raise whatever errors it sees fit. You should make explicit choices about what happens.

When you're appending the string to playlist_entries, it's quite hard to read. You would do better to define an ENTRY_TEMPLATE string at the top of the file which you just use and format when appending.

Python truthiness can be very useful, but in this case if i is liable to confuse people. I would suggest either an explicit statement like i != 0 or a comment.

    if i:  # Not the first entry