Returning the TV to its original state

I need help with setting up TV automation. I can’t implement the function myself. I need to make the TV remember the current state it is in (let’s say YouTube is running), after which the video I need would open on the TV (20 seconds long), after the video has played, how can I make the TV return to its original state (Youtube)? I have already implemented the video launch, I just need to figure out how to save the state and return to the original state

Look what data you have available in the developer tools.
Depending on the TV and its capabilities you might be able to fully restore the state, because the YouTube is sometimes an app and the TV only have the ability to start the app, but not to actually control anything inside it, like selecting a stream and starting the play.

Related thread

I think if Youtube was launched, and on top of it I give the command to launch another application, if I return to Youtube, then it will be displayed in the last state, whether it is watching a video or broadcasting, no?
Tell me, where can I find data with which I can work on the TV?

How the TV react to actions are up to the manufacturers.
Use the HA developer tools and look up the state of your TV sensors.

Might be possible using scene.create action if your automation creates the scene before switching to your other video