Skip to content

initial-speaker-cli#26

Open
stanphong wants to merge 2 commits into
masterfrom
fastAPI-0404
Open

initial-speaker-cli#26
stanphong wants to merge 2 commits into
masterfrom
fastAPI-0404

Conversation

@stanphong

@stanphong stanphong commented Apr 6, 2025

Copy link
Copy Markdown
Contributor

This PR implements a FastAPI version of the audio player functionality.

The project has two main components:

  • audio43.py: Contains the original command-line audio player functionality
  • main.py: Implements the same functionality as a FastAPI web service

The implementation:

  • Uses command-line execution for yt-dlp and mpv player
  • Implements an MPV queue system (using a list structure) that limits concurrent playback to 3 URLs for efficient handling
  • Provides background task processing for non-blocking audio playback

Comment thread fastAPI/audio43.py Outdated
print(f"Finished playing {played_count}/{total_count} videos")

if __name__ == "__main__":
playlist_url = 'https://www.youtube.com/playlist?list=PLyLqO_HeaCB5QxDs04P0un3SAyfGYS-GW'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no hard coded url plsssss, if you want to include some functionality for testing that is okay, but you would then need to use arge parsing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated using arge parsing with an option -u for the url

Comment thread fastAPI/main.py Outdated
url: str

@app.get("/")
def root():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont return hello world here, either dont have a route for / or implement some type of functioanlity

i prefer not having an api route for / as just by looking at it idk what it is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have deleted the root / for now

Comment thread fastAPI/main.py Outdated
audio_url = playing_queue.pop(0) # Get the next URL
played_count += 1

print(f"Playing video {played_count}/{total_count}: {audio_url[:50]}...")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

50?

@stanphong stanphong Apr 21, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it in audio43.py and use the functions from it in here

Comment thread fastAPI/main.py Outdated

app = FastAPI()

class PlaylistRequest(BaseModel):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the use of BaseModel USUALLY but in this case when we only have 1 variable in the class, importing a whole library for this functioanlity actually kind of defeats the purpose of BaseModel

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed it, and just simply use a direct argument for url

Comment thread fastAPI/audio43.py Outdated
audio_url = playing_queue.pop(0) # Get the next URL
played_count += 1

print(f"Playing video {played_count}/{total_count}: {audio_url[:50]}...")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

50?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated with a global constant. It is for limiting the number of char when displaying the url

Comment thread fastAPI/audio43.py Outdated
playing_queue.append(next_url)
print(f"Added next video to queue. Remaining: {len(remaining_queue)}")
except Exception as e:
print(f"Error playing {audio_url[:50]}: {str(e)}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we write the errors to a file? printing when no one is looking at the logs is useless. and if pi gets turned off or somethign we have the lgos saved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I move the errors to errors.log instead in the updated code

Comment thread fastAPI/audio43.py Outdated

def get_youtube_audio_urls(playlist_url):
# Create a list to store the URLs
audio_urls = []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in updated code

Comment thread fastAPI/main.py Outdated

def get_youtube_audio_urls(playlist_url):
# Create a list to store the URLs
audio_urls = []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment thread fastAPI/audio43.py Outdated
@@ -0,0 +1,73 @@
import subprocess
import queue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused imports?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants