Создайте список воспроизведения для vl c из audi-cd с python3 для RaspBerry Pi 4+ - PullRequest
0 голосов
/ 08 мая 2020

Надеюсь, кто-нибудь старомодный вроде меня, которому также нравятся аудио-компакт-диски, может дать мне несколько советов, как правильно с ними обращаться.

Я создал простой проигрыватель компакт-дисков, который работает на моем Raspberry Pi 4+ с сенсорный экран 7 дюймов, но я не очень доволен своим решением.

Все работает, но я не могу поверить, насколько сложно было создать плейлист. На мой взгляд, должен быть гораздо более элегантный и изощренный способ сделать это.

Если у кого-то есть идея, я был бы рад услышать об этом. Виноват себя, вот мой код:

#!/usr/bin/python3
import vlc
import time
import re
import sys

# my favorite player
instance = vlc.Instance()
player = instance.media_player_new()

# not too loud for my spouse
player.audio_set_volume(50)

# CDDA adress in Windows for VLC to play my cd rom tracks
# for raspberry it will look like cdda:///dev/sr0, .cdda-track=track_nr
url = "cdda:///G:"      # will be changed on RaspBerry Pi
opt = ":cdda-track="    # will be expanded to :cdda-track=track_nr

playlist = []

# reading the table of contents (toc), created with cdparanoia -Q
# This is the result of the UNIX command cdparanoia -Q

#cdparanoia III release 10.2 (September 11, 2008)
#
# 
#
#Table of contents (audio tracks only):
#track        length               begin        copy pre ch
#===========================================================
#  1.    22190 [04:55.65]       55 [00:00.55]    no   no  2
#  2.    11350 [02:31.25]    22245 [04:56.45]    no   no  2
#  3.    20567 [04:34.17]    33595 [07:27.70]    no   no  2
#  4.    23500 [05:13.25]    54162 [12:02.12]    no   no  2
#  5.    16985 [03:46.35]    77662 [17:15.37]    no   no  2
#  6.    27358 [06:04.58]    94647 [21:01.72]    no   no  2
#  7.    15880 [03:31.55]   122005 [27:06.55]    no   no  2
#  8.    38797 [08:37.22]   137885 [30:38.35]    no   no  2
#  9.    11043 [02:27.18]   176682 [39:15.57]    no   no  2
#TOTAL  187670 [41:42.20]    (audio only)

track = []      # track-Nr.
max_track = 0   # last track
dur = []        # playtime
for_safety_reasons = 1 # for safety reasons
toc = 'D:/Python-Programme/gui-beispiel/CD-Project/toc.txt'

# read toc as an object
all_songs = open(toc, 'rt')
# zeile = lieder.readline()
for line in all_songs:
    # filter relevant track information
    if '[' in line and ']' in line and ':' in line and '.' in line and not ('T' or 't') in line:

        # track is in columns 1 to 3
        # removing leading spaces
        track = line[1:3].strip()
        max_track =+1

        # song duration is between the first pair of square brackets [04:55.65]
        beg = line.find('[')+1
        end = line.find(']')
        dur = line[beg:end]

        # duration splitted in minutes, seconds and floating rest
        # and as sum calculated, with respect of the 10 value
        min = 10 * int(dur[0]) + int(dur[1])
        sec = 10 * int(dur[3]) + int(dur[4])
        rst = (10 * int(dur[6]) + int(dur[7]))/100
        playtime = 60 * min + sec + rst     # add the results

        # all songs are stored in a list as follows
        # track, duration [s], duration mm:ss.rr
        # [string, float, string], [..., ..., ...] ...
        song = [track, playtime, dur]

        # store result in the playlist
        playlist.append(song)

# for testing purposes, I need the seconds and
# duration for my user interface to inform about the songs
# print('song(1)          ', playlist[0])
# print('song(1) track.   ', playlist[0][0])
# print('song(1) seconds  ', playlist[0][1])
# print('song(1) duration ', playlist[0][2])

# creating an url for each track in playlist
# vlc needs to play a song (cdda:///dev/sr0, cdda_track=track_nr) as url/mrl
song_nr = 0
for song in playlist:
    print("listening to:", url, opt + playlist[song_nr][0])
    # create the above mentioned url/mrl
    song = instance.media_new(url, opt + playlist[song_nr][0])
    player.set_media(song)  # assign the song
    player.play()           # play the song

    time.sleep(playlist[song_nr][1] + for_safety_reasons)
    player.stop()           # may be not necessary
    song_nr += 1            # next song
...