【问题标题】:Getting song name of streaming track in iTunes with Applescript使用 Applescript 在 iTunes 中获取流媒体曲目的歌曲名称
【发布时间】:2012-05-04 05:46:08
【问题描述】:

当当前曲目是收音机时,如何在 iTunes 中获取当前曲目的歌曲名称?

我的意思是出现在电台名称正下方的字符串:)

像下面这样查询它的名字会给我收音机的名字 (Trance Channel - DIGGITALLY IMPORTED - we can't define it!) 但不是歌曲

tell application "iTunes"
    set thisTrack to current track
    set trackName to the name of thisTrack
    set trackTime to thisTrack's time
end tell

这是预期的,因为我的图书馆中的信息是: 但是有没有办法专门处理这些流媒体曲目?并像第一张图片中的 iTunes 一样正确获取他们的信息?我知道当前曲目是收音机,因为它的时间将是 missing value 而不是 MM:SS 的形式,如果这有帮助的话。

这可能吗?

【问题讨论】:

    标签: macos applescript itunes


    【解决方案1】:

    看来,在 iTunes 12.2 中,各种有趣的事情正在发生。

    1. current stream title 在请求来自“For You”的流名称时返回missing value(例如,当前音乐库中不是的东西)。 name of the current track 不存在。例如,我现在正在听“For You”中的“Alternative Gems: 1994”(Yay-grad 学生时代),但我无法获得任何关于正在播放的信息。如果我转到正在播放曲目的专辑以播放其他内容,missing valuename of current track 上的错误 -1728 也是。

    2. 当按照上面的@ivan 收听 Beats 1 广播时,我也会收到 missing value 但是 对于 name of the current track 我会收到“Beats 1”。正如@dougscripts 指出的那样,stream title 的内容在地图上各不相同。

    3. 收听通过“For You”创建的广播电台似乎给了我正确的name of the current track

    所以,简而言之,就是混乱。

    【讨论】:

    • 你得到 Beats 1 的分辨率了吗?
    • 我设法找到了处理 Beats 1 stackoverflow.com/a/50917619/580513 的解决方案
    • 经典 FM(英国商业广播电台)流也有奇怪的行为 - iTunes 偶尔会显示诸如“ADBREAK_1234567890....”之类的内容,但 流标题为文本 是 '在那一刻缺少价值。另外,我有时会发现,当 iTunes 全屏时,屏幕上显示的元数据有时会丢失,但在查看迷你播放器时会出现。
    【解决方案2】:

    另一个选项 - 考虑到输出的不一致 - 可能是在 VLC 中播放并进行查询 - AppleScript 支持非常有限,但至少有 the source code 可用,因此您知道可以查询什么。

    osascript -e 'tell application "VLC" to get name of current item'
    

    【讨论】:

      【解决方案3】:

      我查看了 iTunes 的 applescript 字典并搜索了“流”...

      tell application "iTunes"
          current stream title
      end tell
      

      【讨论】:

      • 我需要了解那些字典... :) 谢谢是一种魅力!
      • @IvanChau 你找到解决方案了吗?
      • 我一直在尝试寻找一种方法来读取显示曲目和艺术家的 browserWindow 视图属性。 iTunes 应用程序能够读取流详细信息,因为它被编码到流中。遗憾的是,这在简单的 API 上不可用。如果我有可能找到一条不同的路线,我会分享我的发现。如果有人知道如何将窗口直接读取到问题上的指向元素,那就太好了!
      • @IvanChau 我设法找到了一个适用于 Beats 1 并且合理的解决方案。 stackoverflow.com/a/50917619/580513
      【解决方案4】:

      更多的黑客攻击,我终于找到了一种使用 SDK 直接从 iTunes 中获取数据的方法。

      这种方法会像正常一样检查currentTrack,但是当检测到artist 丢失时(在流式传输轨道时的常见理解),我们就只能使用值从 LCD 显示器获取值由可访问性提供。

      #!/usr/bin/env osascript -l JavaScript
      /* globals Application */
      function main () {
        var itunes = new Application('iTunes')
        var currentTrack = itunes.currentTrack
        var output = {
          name: currentTrack.name(),
          artist: currentTrack.artist(),
          position: itunes.playerPosition()
        }
        if (currentTrack.artist() === '') {
          var app = new Application('System Events')
          var itunesProcess = app.applicationProcesses.byName('iTunes')
          // Get the text values from the first scrollable area which is the LCD Display
          var arr = itunesProcess.windows[0].scrollAreas[0].staticTexts
      
          output.name = arr[0].name()
          // Clean up the artist name, as it may contain the Show Name. 
          output.artist = arr[2].name().replace(' — ' + currentTrack.name(), '')
        }
        return JSON.stringify(output, null, 2)
      }
      main()
      

      示例输出:

      {
          "name": "Wild Smooth (Gundam Radar Rip)",
          "position": "34:06",
          "artist": "MUBLA"
      }
      

      请务必chmod +x此脚本。

      注意,需要将调用应用添加到Accessibility Privacy

      【讨论】:

        【解决方案5】:

        并非所有的流媒体都会对流数据进行相同的格式化,因此当前流标题属性的结果可能不一致。

        【讨论】:

          【解决方案6】:

          经过数周的努力,我试图自己解决这个问题。 我已经设法为这个确切的问题找到了一个非常hacky的解决方案。

          但你不会喜欢它,请注意

          即使音频是从 Beats 收音机流式传输的,为了获得当前播放曲目,您将不得不考虑 OCR 方法(截屏,将图像转换为文本)

          以下 Ruby 代码将帮助您开始使用此解决方案。

          它会检查当前的轨道,如果artist字段为空,(当轨道被流式传输时就是这种情况)那么它就属于OCR方法了。

          require 'json'
          require 'rtesseract'
          class CurrentTrack
            def self.check
              js_command = %Q{var itunes = Application("iTunes");
              var currentTrack = itunes.currentTrack;
              JSON.stringify({
                window_bounds: itunes.windows[0].bounds(),
                name: currentTrack.name(),
                artist: currentTrack.artist(),
                position: itunes.playerPosition()
              })
              }
              command = "osascript -l JavaScript -e '#{js_command}'"
              result =  `#{command}`
              json = JSON.parse(result, symbolize_names: true)
              json[:position] = json[:position].to_i
              json[:cue] = Time.at(json[:position]).utc.strftime('%H:%M:%S')
              if json[:artist] == ''
                sc_command = %Q{screencapture -R #{json[:window_bounds][:x]},#{json[:window_bounds][:y].to_i + 30},#{json[:window_bounds][:width]},#{json[:window_bounds][:height]} capture.png}
                `#{sc_command}`
                image = RTesseract.new("capture.png", processor: 'none')
                ocr = image.to_s.split("\n") # Getting the value
                unless ocr.first == 'Soulection'
                  json[:name] = ocr.first
                  json[:artist] = ocr[1].split(' — ').first
                end
              end
              json.delete :window_bounds
          
              json
            end
          end
          

          您需要安装 rtesseract 才能使其正常工作。

          注意,此脚本要求 iTunes 迷你播放器窗口在您桌面的某个位置可见。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-01
            • 2017-05-30
            • 2016-04-22
            • 1970-01-01
            • 2011-06-04
            • 1970-01-01
            相关资源
            最近更新 更多