I second using sonarr/radarr, once imported it detects episodes and lets you one click rename to a specific format and folder organization.
If you don't want any of the other features of sonarr/radarr (like having a way to filter and manage your collection to see what's in what quality or from what release group, searching multiple indexers with a single search, being able to send a specific search result to a downloader and have it automatically imported and organized when complete, or have auto downloading based on requests using scoring rules that you set), then there's also filebot which a lot of people seem to like and seems to be just for matching with online metadata and renaming.
But I haven't tried filebot since I like the extra features and capabilities of sonarr/radarr. It makes it easy to manage several library folders like an archive for anything that's been reviewed, is complete, and in a quality/codec that I'm satisfied with, and keeping track of currently airing shows in my active folder which is where I also keep auto downloaded stuff I haven't reviewed.
The same way filebot and any other tool does - the file needs to have some label, either an absolute episode number or a season + episode number. I'm not aware of any tool that is able to look at the contents of the video to figure out which episode it is visually without any information from the filename - but I'd be happy to be proven wrong because I would be impressed.
Sonarr/radarr does analyze the content somewhat but that's just for gathering resolution, codec, HDR, audio languages, and subtitle information, which can all be added to the filename format for inclusion during renaming.