0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mpvで最後まで再生したファイルを移動

Posted at

動機

  • ラジオ番組を録音したオーディオファイルなどは、最後まで再生した後は移動して、二度聴きを防止したい。
  • 最後まで再生していないファイルは再度再生したい。
  • 音楽ファイルなどはラジオ録音ファイルと異なり、何度も再生したい。
  • 安全のため、ある程度メジャーなプレーヤ(オープンソース、いろんなOSで動く等)で実現したい。

解決策

オープンソースのmpvプレーヤを、jsでカスタマイズする。

~/.config/mpv/scripts/my.js
var curPath = null;
// ファイルパスの先頭が一致した場合のみ、移動する。
function mvfile(prefix, to) {
  if (curPath.indexOf(prefix) === 0) {
    mp.utils.subprocess({args: ["mv", curPath, to], playback_only: false});
    // mp.utils.subprocess({args: ["cmd", "/C", "move", curPath, to], playback_only: false}); // for Windows
  }
}
// ファイル読み込み後に呼び出されるイベント。
// end-fileイベント内で取得できないため、ファイルパスをこのタイミングで取得する。
mp.register_event("file-loaded", function() {
  curPath = mp.get_property_native("path");
});
// ファイルクローズ時に呼び出されるイベント、先頭パターンと移動先を複数指定している。
mp.register_event("end-file", function(event) {
  if (event.reason == "eof") { // 最後まで再生された場合
    mvfile("sd/radio/", "sd/backup/mvfiles/"); // 引数は例
    mvfile("r/", "r/mvfiles/"); // 引数は例
  }
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?