Re: [Dorset] Ending Playback in mpg321 from Within Python
Hi Terry, > > subdir = './Playlist1' > > mp3_files = sorted(os.path.join(subdir, x) for x in > > filter(lambda f: f.lower().endswith(".mp3"), os.listdir(subdir))) > > Thanks Will, that worked. If it makes it easier to understand and adapt, the non-functional unwinding of it is subdir = './Playlist1' mp3 = [] for f in os.listdir(subdir): if f.lower().endswith('.mp3'): path = os.path.join(subdir, f) mp3.append(path) mp3.sort() Cheers, Ralph. -- Next meeting: Bournemouth, Tuesday, 2017-02-07 20:00 Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/ New thread: mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING Reporting bugs well: http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR
Re: [Dorset] Ending Playback in mpg321 from Within Python
On Tuesday, 10 January 2017 12:31:52 GMT William R Sowerbutts wrote: > You need to add the directory prefix to the result of os.listdir(). > > So if you do os.listdir('foo') it will return ['bar', 'jam']. When you pass > these to mpg321 you want the directory name prefixed ie ['foo/bar', > 'foo/jam']. > > So try this: > > subdir = './Playlist1' > mp3_files = sorted(os.path.join(subdir, x) for x in filter(lambda f: > f.lower().endswith(".mp3"), os.listdir(subdir))) Thanks Will, that worked. -- Terry Coles -- Next meeting: Bournemouth, Tuesday, 2017-02-07 20:00 Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/ New thread: mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING Reporting bugs well: http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR
Re: [Dorset] Ending Playback in mpg321 from Within Python
Hi Terry, > mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), > os.listdir('./ Playlist1'))) Just about to be AFK, but this might help. $ find . ./foo ./foo/bar $ python -c 'import os; print(os.listdir("foo"))' ['bar'] $ You get given bar but it's foo/bar to open it from the higher level. Cheers, Ralph. -- Next meeting: Bournemouth, Tuesday, 2017-02-07 20:00 Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/ New thread: mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING Reporting bugs well: http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR
Re: [Dorset] Ending Playback in mpg321 from Within Python
Terry You need to add the directory prefix to the result of os.listdir(). So if you do os.listdir('foo') it will return ['bar', 'jam']. When you pass these to mpg321 you want the directory name prefixed ie ['foo/bar', 'foo/jam']. So try this: subdir = './Playlist1' mp3_files = sorted(os.path.join(subdir, x) for x in filter(lambda f: f.lower().endswith(".mp3"), os.listdir(subdir))) Best wishes Will On Tue, Jan 10, 2017 at 12:21:14PM +, Terry Coles wrote: >On Sunday, 6 November 2016 14:37:53 GMT William R Sowerbutts wrote: >> Whipped this quick demo up; it will play at most five seconds of each MP3 >> file in the current directory. Tested with both mpg321 and mpg123. > >Will, > >The code you gave me a month or two back works beautifully, except I want >to have various Playlists in various directories whilst also playing the >chimes using a separate function. In other words, the chimes are in another >directory, so I really want to stay at the top-level directory and play the >files in various sub-directories. Your code, as it stands, relies on the MP3 >files being in the current directory. > >Not a problem I thought (naively); I'll just substitute the required directory >into the relevant line and bobs-your-uncle! So your: > >mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), >os.listdir('.'))) > >became: > >mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), os.listdir('./ >Playlist1'))) > >The problem is that the code clearly enters the directory and prints the name >of the files, but it doesn't play anything and finishes the loop in about half >a >second! What am I missing? > > >> --- >> #!/usr/bin/env python >> >> import os >> import time >> import subprocess >> >> mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), >> os.listdir('.'))) blackhole = open(os.devnull, 'w') >> for file in mp3_files: >> print("Playing %s ..." % file) >> # launch the player >> player = subprocess.Popen(['mpg321', '-q', file], stdin=subprocess.PIPE, >> stdout=blackhole, stderr=blackhole) # play at most 5 seconds, or until end >> of file, whichever is first: wait_until = time.time() + 5 >> while time.time() < wait_until: >> player.poll() # check to see if terminated >> if player.returncode != None: >> break >> time.sleep(0.1) >> # kill the player if it's still running >> if player.returncode == None: >> player.terminate() >> player.wait() > >-- > > > > Terry Coles > >-- >Next meeting: Bournemouth, Tuesday, 2017-02-07 20:00 >Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/ >New thread: mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING >Reporting bugs well: http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR _ William R Sowerbutts w...@sowerbutts.com "Carpe post meridiem" http://sowerbutts.com main(){char*s=">#=0> ^#X@#@^7=",c=0,m;for(;c<15;c++)for (m=-1;m<7;putchar(m++/6%3/2?10:s[c]-31&1<
Re: [Dorset] Ending Playback in mpg321 from Within Python
On Sunday, 6 November 2016 14:37:53 GMT William R Sowerbutts wrote: > Whipped this quick demo up; it will play at most five seconds of each MP3 > file in the current directory. Tested with both mpg321 and mpg123. Will, The code you gave me a month or two back works beautifully, except I want to have various Playlists in various directories whilst also playing the chimes using a separate function. In other words, the chimes are in another directory, so I really want to stay at the top-level directory and play the files in various sub-directories. Your code, as it stands, relies on the MP3 files being in the current directory. Not a problem I thought (naively); I'll just substitute the required directory into the relevant line and bobs-your-uncle! So your: mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), os.listdir('.'))) became: mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), os.listdir('./ Playlist1'))) The problem is that the code clearly enters the directory and prints the name of the files, but it doesn't play anything and finishes the loop in about half a second! What am I missing? > --- > #!/usr/bin/env python > > import os > import time > import subprocess > > mp3_files = sorted(filter(lambda f: f.lower().endswith(".mp3"), > os.listdir('.'))) blackhole = open(os.devnull, 'w') > for file in mp3_files: > print("Playing %s ..." % file) > # launch the player > player = subprocess.Popen(['mpg321', '-q', file], stdin=subprocess.PIPE, > stdout=blackhole, stderr=blackhole) # play at most 5 seconds, or until end > of file, whichever is first: wait_until = time.time() + 5 > while time.time() < wait_until: > player.poll() # check to see if terminated > if player.returncode != None: > break > time.sleep(0.1) > # kill the player if it's still running > if player.returncode == None: > player.terminate() > player.wait() -- Terry Coles -- Next meeting: Bournemouth, Tuesday, 2017-02-07 20:00 Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/ New thread: mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING Reporting bugs well: http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR