Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you get current play position programmatically? #3370

Open
soufriere opened this issue Mar 28, 2021 · 5 comments
Open

How do you get current play position programmatically? #3370

soufriere opened this issue Mar 28, 2021 · 5 comments
Labels
plugin Can be implemented as a plugin

Comments

@soufriere
Copy link

Hello, I'm a transcriber and I used to use VLC for transcriptions. I have to insert timestamps into my transcript, so I have an applescript that can copy / paste the current VLC play position into my transcript, like this: [00:03:32].

Is there any way to access the play position programmatically in IINA? A terminal command or so? If the play position were in the window title I could access it via applescript, but it doesn't show it in the window title either...

@low-batt
Copy link
Contributor

Something like this?:

low-batt@gag ~$ echo '{ "command": ["get_property", "time-pos"] }' | socat - /tmp/mpv-socket | jq '.data | strftime("%T")'
"00:01:04"
low-batt@gag ~$ 

I used Homebrew to install socat and jq.

Then I brought up IINA and selected Preferences / Advanced and enabled advanced settings. Under "Additional mpv options" I clicked on + and added:

Name Value
input-ipc-server /tmp/mpv-socket

I then restarted IINA for the changes to take effect.

IINA is a graphical user interface for the mpv media player. That player is designed for command line use and has no real GUI, only a small controller on top of the video for basic control. IINA provides a full featured GUI for mpv. Many mpv features are available in IINA. From the mpv manual:

--input-ipc-server=<filename>

Enable the IPC support and create the listening socket at the given path.

This allows us to use mpv's JSON IPC API. See that section of the manual for examples. This sends a command to get the current position in seconds:

low-batt@gag ~$ echo '{ "command": ["get_property", "time-pos"] }' | socat - /tmp/mpv-socket
{"data":64.254000,"request_id":0,"error":"success"}
low-batt@gag ~$ 

Then jq is used to parse the JSON, extract the value of the data key and convert the seconds to a time string.

@ELgit
Copy link

ELgit commented Jun 21, 2022

Wouldn't it be easier to just have playback time as an optional sticky display in IINA so when someone is transcribing or taking notes on the video in a separate app all they have to do is glance at the playback time and write it down?

Given that's not an option/feature, I tried the instructions above.

There are some assumed steps that I (a non-programmer) am not aware of so it's not working for me.
This is what I did:

  • installed home-brew, socat and jq using the terminal commands from the links/pages provided.
  • added the advanced settings per instructions and restarted IINA
  • read through the "--input-ipc-server=" info -- is that a command I invoke somewhere? if so that's not stated
  • I don't understand how to use JSON IPC API to send the command
  • I don't understand how to tell JSON IPC API to send the command
  • after the command is sent, I don't understand how to tell JQ to parse, extract, convert to time string
  • I don't understand how to do a keyboard shortcut to invoke everything above and past the time string into the word document I'm using to transcribe the video.

Any help greatly appreciated.

@low-batt
Copy link
Contributor

@ELgit Yes, my above instructions could use some more details. Don't worry, we will get this to work for you.

The following is a partial answer. I need to get this working using Automator. Using Automator we should be able to automate this so that one key press inserts the time. Still working on that. Ran out of time today. For now lets get this working for you from terminal.

Wouldn't it be easier to just have playback time as an optional sticky display in IINA so when someone is transcribing or taking notes on the video in a separate app all they have to do is glance at the playback time and write it down?

Yes. I'm sure we could come up with an easier way to do this with a new feature. But the question posed by this issue was not about that. So the answer given addresses how to do this with IINA as it is today. A request for a new IINA feature to make this easier should be entered as a new feature request issue. There are plans to add AppleScript support. That might make this easier.

I should have included a picture showing how to use input-ipc-server. Bring up IINA/Preferences. Click on the Advanced tab. Slide the Enable advanced settings toggle button to be on (blue). In the Additional mpv options table click on the + button. That will add an entry to the table with the values name and value. Click on name and change that to input-ipc-server. Click on value and change that to /tmp/mpv-socket. When finished the preferences window should look like:

issue-3370-prefereneces

Then restart IINA.

This setting causes IINA to open a socket for inter-process communication (IPC) at /tmp/mpv-socket and listen for commands sent to that socket. This provides a way to control IINA from terminal.

Once IINA is listening for commands then while playing a movie you should be able to paste the following command sequence into terminal:

echo '{ "command": ["get_property", "time-pos"] }' | socat - /tmp/mpv-socket | jq -r '.data | strftime("%T")' | pbcopy

After running that command pasting into a TextEdit window added this to the document:

00:00:24

To update the pasteboard with the press up arrow in terminal to recall the previous command and then press return again.

The technologies involved in the command sequence:

  • pipeline A sequence of commands connected by the pipe operator (|)
  • JSON A standard for passing data as name/value pairs
  • echo Outputs a JSON formatted string to socat
  • socat Writes to and reads from IINA
  • jq Parses the JSON returned by IINA and extracts and formats the time
  • pbcopy Copies the text to the pasteboard

The next step is to get something like this running under Automator and assign a shortcut key. I will investigate that.

@lhc70000
Copy link
Member

This can be easily done under the plugin system, which should be available in 1.4.0. We can release a beta version as soon as it's merged.

@lhc70000 lhc70000 added the plugin Can be implemented as a plugin label Jun 22, 2022
@low-batt
Copy link
Contributor

As @lhc70000 indicated this will be easier to accomplish once planed features are added to IINA in an upcoming version.

With IINA 1.3.0, using Automator I was able to configure a shortcut key and paste into TextEdit the current playback position. To set this up perform the following actions:

  • Open Automator

  • Click on Quick Action:
    issue-3370-quick-action

  • In the Workflow receives pull down select no input

  • Scroll or search to find Run AppleScript and drag that to the workflow

  • Entirely erase the pre-populated script that starts with on run {input, parameters}

  • Copy and paste in the following script:

do shell script "export PATH=\"$PATH:/opt/homebrew/bin\";echo '{ \"command\": [\"get_property\", \"playback-time\"] }' | socat - /tmp/mpv-socket | jq -r '.data | strftime(\"%T\")' | pbcopy"
tell application "System Events" to keystroke "v" using command down
  • Click File/Save and save the workflow using the name IINA Paste Playback Time

  • Automator should now look like:
    issue-3370-automator

  • Open System Preferences/Keyboard

  • Click on Shortcuts

  • Scroll down to find IINA Paste Playback Time

  • Assign ⌃⌘T (control command T) as the shortcut

  • System Preferences should look like:
    issue-3370-shortcut

  • Open System Preferences/Security & Privacy

  • Click on Accessibility

  • Click on + and add Automator

  • Click on + and add ScriptEditor

  • Click on + and add TextEdit (for testing)

  • Click on + and add the application you are using to write notes

To test this is working open IINA and play a video. Open TextEdit. Make sure the keyboard focus is in the TextEdit window. Press ⌃⌘T. The current playback time appear in the TextEdit window.

The tricky part is to insure the script is not blocked by macOS security, thus the need to grant applications permissions in Security & Privacy. It is also critical that the shortcut key chosen is not already used by the applications used for editing. If pressing the key does not work you can invoke the script directly under the Services menu. If that works then suspect the application already uses that key and try a different shortcut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin Can be implemented as a plugin
Projects
None yet
Development

No branches or pull requests

4 participants