ユーザ用ツール

サイト用ツール


サイドバー

Top

検索タグ

変更履歴

メニュー


python:python-vlc

python-vlc

リアル世界でのVLC
ごめんなさい🙏ブラ男氏が縄張りを主張しちゃいました🐶😅💦

本家: Python bindings for VLC
ドキュメント: API Documentation

インストール

> pip install python-vlc
Collecting python-vlc
  Downloading python_vlc-3.0.7110-py3-none-any.whl (80 kB)
     |████████████████████████████████| 80 kB 721 kB/s
Installing collected packages: python-vlc
Successfully installed python-vlc-3.0.7110

メディアリストのループ再生

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import vlc
import os
import mutagen
 
# 拡張子は小文字で定義 !!
#exts = ('.mp3', '.m4a', '.wav', '.wma', '.mpg', '.mov', '.mkv', '.mp4', '.webm')
exts = ('.mp3', '.m4a', '.flac', '.wav')
 
PATH = '/var/samba/DataShare/WinAmpData/TRF'
 
def walk(dir, exts):
    result = []
    for path, dirs, files in os.walk(os.path.join(dir, '.'), followlinks=True):
        for file in files:
            #print(file)
            if file.lower().endswith(exts):
                dir_path = os.path.abspath(path)
                result.append(os.path.join(dir_path, file))
 
    return result
 
def printVLCMeta(media):
    title = media.get_meta(vlc.Meta.Title)
    artist = media.get_meta(vlc.Meta.Artist)
    album = media.get_meta(vlc.Meta.Album)
    trackNumber = media.get_meta(vlc.Meta.TrackNumber)
    trackNumber = trackNumber if not trackNumber is None else 0
    trackTotal = media.get_meta(vlc.Meta.TrackTotal)
    trackTotal = trackTotal if not trackTotal is None else 0
    genre = media.get_meta(vlc.Meta.Genre)
    #url = media.get_meta(vlc.Meta.URL)
 
    print(f'Album: {album}')
    print(f'Title: {title}')
    print(f'Artist: {artist}')
    print(f'Genre: {genre} Track: {trackNumber: >3}/{trackTotal}')
    #print(f'URL: {url}')
 
def main():
    medias = walk(PATH, exts)
    medias.sort()
    print(f'Media Count: {len(medias)}')
 
    mediaList = vlc.MediaList()
 
    for index, media in enumerate(medias):
        print(f'Index: {index: >4} Path: {media}')
        mediaList.add_media(media)
 
    mediaListPlayer = vlc.MediaListPlayer()
    mediaListPlayer.set_media_list(mediaList)
    mediaListPlayer.set_playback_mode(vlc.PlaybackMode.loop)
    mediaListPlayer.play()
 
    mediaPlayer = mediaListPlayer.get_media_player()
    mediaPlayer.audio_set_volume(30)
 
    while True:
        print('po: POSITION, i: INDEX/Info, p: PREVIOUS, n: NEXT, q: QUIT')
        data = input()
 
        #POSITION
        if data == 'po':
            pos = mediaPlayer.get_position()
            print(f'Position: {pos}')
 
        #INDEX/Info
        elif data == 'i':
            media = mediaPlayer.get_media()
            path = media.get_mrl()
            #ft = mutagen.File(path)
            #title = ft['title']
            index = mediaList.index_of_item(media)
            print(f'Index: {index}')
            printVLCMeta(media)
 
        #PREVIOUS
        elif data == 'p':
            mediaListPlayer.previous()
            media = mediaPlayer.get_media()
            index = mediaList.index_of_item(media)
            fileName = media.get_meta(0)
            print(f'Previous -> Current Index: {index} File: {fileName}')
 
        #NEXT
        elif data == 'n':
            mediaListPlayer.next()
            media = mediaPlayer.get_media()
            index = mediaList.index_of_item(media)
            fileName = media.get_meta(0)
            print(f'Next -> Current Index: {index} File: {fileName}')
 
        #QUIT
        elif data == 'q':
            mediaListPlayer.stop()
            print('Quit.')
            break
 
if __name__ == '__main__':
    main()

* os.walk() に followlinks=True オプションを追加。(シンボルリンクを辿るように指定)

参考文献

python/python-vlc.txt · 最終更新: 2021/05/05 03:06 by ともやん