1. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    If your android box is rooted, you can use WiFi ADB to auto enable adb after every boot (if it doesnt stay enabled in your case), also if for some reason the adb server hangs, this can be used to reset it in two clicks.

    https://play.google.com/store/apps/details?id=com.ttxapps.wifiadb

    (Integrated it in the adb setup tutorial as well.)

    edit: To not bump the thread just for that - here is also

    How you enable high resolution videos (1080p and above) in the Kodi Youtube plugin

    First make sure InputStream Adaptive is installed and enabled, Add-ons > My Add-ons > VideoPlayer InputStream > InputStream Adaptive

    If its not on your device already, you should be able to install it through the official Kodi repo. If thats not the case, google for a package to install it on your specific platform (Android).

    Then configure InputStream Adaptive; Ignore Display Resolution: enabled, Max. Resolution general decoder: Max, Stream Selection: 4k, 1440p or 1080p.

    After that you only have to enable it in the youtube app:
    YouTube addon settings > MPEG-DASH > Use MPEG-DASH: enabled, MPEG-DASH > Use for videos: enabled. Also enable it for live streams. Exit the youtube plugin, enter it again, and it should work. :)

    and here is

    How you create and enter your own API ID in the youtube plugin settings

    so you never get "request limit reached" errors again - this time its just me linking to a badly voiced youtube video someone else made.. ;)
    h**ps://www.youtube.com/watch?v=lxPxMZbDgec
    --

    edit: One more small edit. Turns out, Kodi can do link completion from a referal link, if that link links to an m3u8 with relative links inside. But then it cant do auto bookmarking on where you left off the video/movie. :) (If you stop it f.e.) So the script (macOS and Android), that does full link completion in m3u8 playlists still is preferable.. :) Details. :)
     
    Last edited by notimp, Jan 31, 2020
  2. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Newer fork of aafm thats still maintained:
    https://github.com/Xerus2000/aafm

    Cant test it currently - but will do so in about a week and post findings. If everything goes well, I'll replace the link to the original in the first posting.

    have fun

    n.
     
  3. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Update to the last posting. Fork just works. :) Clone the repositary as a .zip file (or git clone it directly), place the contents of the src folder in ~/Documents/FireRemote/aafm/ and chmod +x *.py in that folder.

    Had to restart it once (would only work, once it created the config files).

    I wont be updating the link to the original version in the corresponding tutorial thread yet, because I've had some issues with it not displaying UI elements until its window is pulled to the foreground. Have to look into that still. (Is mostly an excuse to not fix the launcher script now.. ;) )
    Will update once I do. :)
    --

    Kodi referer handling

    In case you ever come across a .m3u8 file link that fails to play, sending it to Kodi, and that throws you the error 403 (access denied), when trying to download it on a PC - its likely, that the site wants itself as a referer, before it allows you to download the file (Checks if the last site you visited was itself).

    Luckily, Kodi can handle referers as well. :)

    I'll start with the Android push videolink to Kodi 'n play script.

    Simply take the automate workflow you created from one of the previous threads (the normal one, not the one that replaces relative links in playlists with absolute ones :) ), make a copy of it (duplicate is a menue item for workflows in automate. :) ), navigate to edit the last (third) write to file item. On its first line click on the fx icon right to it (displaying it in function form).

    You should see

    ="\n"

    which we just used to append a new line (\n) at the end of the videolink.

    Edit that so it reads

    ="|referer=http://somesite.com\n"

    save the workflow, and thats it.

    Kodi referer handling works by simply putting |referer=http://somesite.com directly after any video link. Kodi will then use that referer trying to access the link.
    -

    For the macOS based acopy script (that does the same), its as simple as adding the line

    Code:
    sed -i '' '$ s/$/|referer=http:\/\/somesite\.com/' ~/Documents/FireRemote/zwischenablage.m3u
    directly after the
    Code:
    pbpaste > ~/Documents/FireRemote/zwischenablage.m3u
    line in the helper script.

    note that sed -i followed by space and two ' and another space is needed for sed to edit/replace the current file in place on macOS. On linux distros the space''space directly after sed -i might not be needed. :)

    $ simply points to end of line, and 'replaces' it with what comes after. For sed, we have to escape some of the characters such as the / and the . thats, why there are a few more backslashes in the url. ;)

    Have fun. :)

    edit: Just for reference. 99.9% of websites dont require a referer, when accessing videolinks, or .m3u8 playlists. This is really just for the edge case, where one you really like - might do. :)
     
    Last edited by notimp, Feb 19, 2020
  4. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Fixed the aafmtrigger (launcher/closer) app (script-editor) code.

    aafm now gets correctly pulled into the foreground while launching.

    Also rewrote the script slightly to close all other terminal windows before launching aafm (/python), which caused an issue with aafm not rendering, when multiple terminal windows were open, when the script tried to launch aafm.

    Updated the link to the newer aafm fork in the original post as well.

    Its now recommended to update to the fork (if you used the original release of aafm), instead of the original release, because it fixes a few adb shell issues the original release of aafm had (deleting multiple files on the android side now works, f.e.).

    edit: Added a little bit of extra delay to make the launcher more reliable as well.

    Script might not work on the first launch (when you give it permissions) - because timings are not as expected. So launch it again (which should quit terminal and Python (make sure to manually quit terminal if it does not (depends on weither Python gets launched or not))), and then launch it a third time. From the third time on (with permissions granted) it should work as intended.

    With this, everything should be up to date again and working as expected. :)

    edit2: Changed timings once more (increased first delay) to increase reliability.
     
    Last edited by notimp, Apr 13, 2020
  5. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Wrote a mostly unrelated MacOS script yesterday, that might become useful for your Android TV media usage - but only in some edge cases.. ;)

    Its still a good one to learn a bunch of concepts at once, so I'm sharing it.

    Lets say a website is DNS blocked by your ISP - and simply changing over to one of the classic 'free DNS' providers (Google, Cloudflare, ..) doesnt fix this.

    Using Tunnelblick (VPN client) is an option (get a VPN connection to a country where the DNS problem doesnt exist), and if you are connected via Tunnelblick, DNS queries also will go over the VPN, which is ultimately what solves our issue.

    Now - for our browser to request the DNS query for our website again, its sufficient to simply open a Chrome incognito tab (if no incognito tab has been opened so far) - and surf to that site there. Also lets automate url entry.. ;)

    At the same time - if we are using a Mail client linked to Gmail for example - we want to close that before connecting to the VPN, because otherwise we get 'someone knows your password' warning mails from google (because we pop up in a different country (IP address) all of a sudden).

    Also - we want to make all of that reversable (normal operational state, with the email program running, and the VPN not connected), only pressing maybe two buttons.. ;)
    -

    Thats pretty much the point, where - if you are me, you start writing a script... ;) (Apple script, can be saved as an app using Script-Editor (comes with every installation of MacOS))

    Code:
    set x to isAppLoaded("Tunnelblick")
    if x then
        tell application "Google Chrome" to close (every window whose mode is "incognito")
        tell application "Tunnelblick"
            disconnect "Finland"
        end tell
        delay 1.5
        tell application "Tunnelblick" to quit
        tell application "Sparrow"
            activate
        end tell
        delay 1
        tell application "System Events"
            tell process "Sparrow"
                click button 1 of window 1
            end tell
        end tell
    else
        tell application "Sparrow" to quit
        delay 1
        tell application "Tunnelblick"
            connect "Finland"
            get state of first configuration where name = "Finland"
            repeat until result = "CONNECTED"
                delay 1
                get state of first configuration where name = "Finland"
            end repeat
        end tell
        set theURL to "https://mysite.com"
     
        if application "Google Chrome" is running then
            tell application "Google Chrome"
                activate
                make new window with properties {mode:"incognito"}
                delay 1
                set URL of active tab of front window to theURL
            end tell
        else
            do shell script "open -a 'Google Chrome' --args --incognito " & quoted form of theURL
        end if
     
        tell application "Google Chrome" to activate
     
    end if
    
    on isAppLoaded(app_name)
        tell application "System Events"
            set app_list to every application process whose name contains app_name
            if the (count of app_list) > 0 then
                return true
            else
                return false
            end if
        end tell
    end isAppLoaded
    
    Script checks if Tunnelblick (VPN client) is running, if not - it closes the email program (Sparrow), it launches the VPN client, it connects it to Finland (connection name in Tunnelblick), once a connection is made it starts a Chrome incognito window, loading the specified url.

    If you run the script a second time and it checks that Tunnelblick (VPN client) is running, it first closes all Chrome incognito tabs (just for convenience), then it disconnects the VPN, quits Tunnelblick, starts up the email client (Sparrow) again, and presses the close window button (closing the window, but not quitting out of the email client) - so it effectively runs in the background again.

    And this is indeed one of the advantages of using MacOS. ;) (High level of scriptability over your GUI programs as well... :) )
     
    Last edited by notimp, Apr 26, 2020
  6. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Reduced two delays in the acopy shell script (copy an url from a Mac to a Kodi Android box and play it) in my private setup.

    sleep 2.5 > sleep 0.5
    and
    sleep 1.5 > sleep 0.5

    those delays were a leftover, from when I used to run the script on a 2010 Macbook Air. Reducing those two delays saves 3 seconds on execution, on my current Macbook, while still being reliable.

    I left in the old values in the acopy script on the first plage, so it doesnt break for people with slower Macs, but if you run them on a somewhat current model, you can save three seconds on every execution. This adds up.. ;)

    Just fyi.
     
    Last edited by notimp, May 11, 2020
  7. Magsor

    Magsor I am watching you
    Member

    Joined:
    Dec 1, 2010
    Messages:
    1,100
    Country:
    Canada
    And may I ask what is your goal with ths software? What are you trying to do? I thinks it like VR; a path that many have tried to do but in the end its useless.
     
  8. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    I use it daily, so to me its certainly not useless. :) Also, documenting it publicly serves the purpose of a neat backup, thats easily accessible for me from everywhere, whenever I want to borrow a piece of code from any of the scripts.

    But to your question as for 'why'.

    Usability on this is top notch. Dare I say great. ;)

    The main script I still use daily is the 'send an online video file link to Kodi on an Android Box and let it play there'.

    So basically the same thing - you see every online video host do currently with a 'cast to chromecast' button, but rather cast it to Kodi (on any Android (TV) box) - and on pretty much every online video (played back on any website) that is out there, not just on those that have a 'play to chromecast' function built in already.

    This all came out of a 'there has got to be a better way' deliberation, when dealing with Kodi video plugins. Kodi has a vast video plugin ecosystem, but those things break from time to time. And they almost always break at the html scraping part (when a site changes something, ..). So instead of writing a bunch of Kodi video plugins, or getting really into python and learn how to read what the other authors Kodi plugin intended to do, with each and every video plugin you want to fix, or for every site you come across, that has a video embedded in it - at some point I had an 'aha' moment that went as follows:

    1. Almost all web videos are unencrypted .m3u8 or .mp4 streams. Kodi can play those back.
    2. A chrome plugin called video download helper I used for years can get me those links with 99% reliability with only two clicks (a button combo and a click)
    3. Almost all my 'discovery' is done better on my Macbook, or my android phone (discovering a web video I want to watch in an interface on my TV (that isnt youtube leanback) works less well), and I'm mostly in front of my TV using a second screen anyhow.

    (4. The Kodi UI is still what I'd want to use for playback/movie navigation. Hate other player Uis. ;) (Kodi is faster, lets you customize the jump forwards and back intervalls, can be set so when you pause a movie you only get a still image with no ui elements left (time indicator fading out after three seconds)...))
    --

    So I started to write a script that would take the videolink from the clipboard, put it in a file, transfer it to a Android Box running Kodi, and make Kodi play that back. (Thereby have it play back on my TV in less than 5 seconds.)

    And because I didn't want that to work only with just one Android Kodi Box, but with multiple ones in my home potentially - I used terminal history to look up the last Kodi box I was connected to (or at least the last one that was listed in terminal history).
    ---

    That was where all of this started. :)

    The rest then just grew out of it. (Wanted a way to do the same thing from my android phone, and not just my Mac, wanted a way to control Kodi from the Mac, wanted a way to control the entire android box (like you would with a remote), from a Mac, wanted a way to transfer files (in both directions) from and to the Mac, wanted to be able to throw videolinks at both Kodi and the Youtube leanback app on the android tv box (so both could be active in ram and I could switch between them, without losing playback state), ...)

    You should see me, when I'm navigating this in front of my TV. It really works. Its really fast. And really convenient... :)

    edit: Basically, think this:


    But instead of the Nintendo Switch, think living room TV (or Nintendo Switch, or both, or ...), and think at least 3 seconds faster, because I removed those delays, as mentioned above.. ;) And think this with pretty much every web video out there (some require referrers (1% maybe), and we can also hand over those.. ;) ).

    Also the main benefit over 'not just casting your screen' is, that you arent encoding/reencoding video here, you are just casting, you can close the browsertab on the Mac (heck, you could even turn the Mac off.. ;) )), you can do a bunch of different stuff, and the video will still be playing on the TV - only consuming the 4 Watt your Android box needs to play back a web video. And it can be fast forwarded, paused, stopped, ... with your TV remote. (Or the Mac controlling Kodi.. ;)) And it retains the intended framerate (the video does). So casting is better than mirroring, always.



    And an Android (TV) box or stick to do this costs 20-120 USD max. :)
    (I personally prefer the Zidoo X9S (with a Logitech Remote (Harmony Companion), as it also has dd-wrt built into it, which means it will basically always run a ftp server on a second OS in parallel (once set up that way) - which helps with the 'hand over a videolink from an android smartphone' script. :) -- but auto framerate switching on those is a bit kludgy (requires a Kodi fork named zdmc, which I stopped using after a while) - I solved that on the TV side, so as much as I love the Zidoo X9S, I cant recommend it for everyone.. :) ).
    Macbook Air (on the laptop side) costs more, but I like them. What can I say... ;)


    Still useless? ;) Or just hard to wrap your head around at first? (Because you could recommend to people that they use Netflix instead, but it just isnt the same, you see.. ;) ) Also usage experience is better than the youtube apps native video casting to f.e. the youtube lean back app. Meaning, with longer videos, I cast them to Kodi, because I know that skipping back 10 or 30 sec will be easier, pausing will be easier, with Kodi I can use voice control, I can set bookmarks, I can pause and photograph or screencap the screen without a UI element being present. (Great to look at/analyze cinematography.) And I dont have to swipe the youtube pairing on casting a video with the youtube app on the phone away after I'm done using it.

    And its fast and intuitive. But it isnt Netflix, so its useless? ;)

    Should you be new to scripting, part of why you would f.e. script an ftp upload via curl instead of using a ftp program is - speed - because once you defined your upload folder, its usually faster to get a file there, if you are not starting your ftp program first, and then connecting that via a bunch of clicks, then ... If you script something, its just faster.

    And you start doing that, once you see, that you do the same tasks over and over again - and start to wonder if you'd enjoy them more, and therefore maybe even do them more, once you cut the repetitive stuff out, and made the entire process faster. ;)

    Ask me next, how long it takes me to watch any movie in the world on my living room screen. Because of chrome search shortcuts, and certain sources.

    12 seconds. Usually.

    From the time I open up my laptop.

    (I promise I only did it once though. For research purposes... )

    Useless? That depends heavily on your perspective, I'd say.. ;)

    Ask me next, why I wrote a bunch of helper scripts to be able to copy over files to the android box faster. To be able to write and test the main scripts faster. :) Ask me why I wrote scripts to be able to control Kodi from my Macbook? Because I didn't want to look for the TV remote, while doing work on my laptop whilst listening to music in Kodi, wanting to skip a track. (And not wanting to do it in a web UI, but with shortcuts, because then I dont have to look. I just hit the shortcut.)

    Ask something you dont understand, and I'll try to answer it to the best of my ability. But dont just dismiss something outright, because you dont understand it, or because it only has one like.. ;)

    It could turn out, that the person you were talking to knows more about home theatre implementations than you do. But just didn't feel like paying for a prefab solution that would have been worse in many aspects.
     
    Last edited by notimp, May 13, 2020
  9. Magsor

    Magsor I am watching you
    Member

    Joined:
    Dec 1, 2010
    Messages:
    1,100
    Country:
    Canada
    Ok. it's just not clear in your intro. You mainly use this thread as a repository. I am just the type of tempers who goes around trying to help people shaking their value knowledge of the world. If you ain't got no problem then keep up the good work!
    I might add that your typing is impeccable, very nice.
     
    notimp likes this.
  10. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Stitched together another quick way to control an android device (f.e. TV box) from a Mac. This time with mouse and keyboard support. :)

    This relies on Synergy (https://symless.com/synergy) which is a paid app. (9 USD)

    You set up the Synergy server on your Macbook (/Linux Laptop), and on the android side you install this apk:
    https://github.com/dcerisano/synergy-android-7/blob/master/synergy.apk

    You specifically have to install it through adb (= command line), by using the parameters:

    adb install -g -t PATHTOSYNERGYAPK

    Because the package is packed with test keys - otherwise the install wont go through.

    That specific app is a fork of the synergy android beta client, which has the keyboard fixed for newer android versions (synergy beta app is quite old). See: https://members.symless.com/forums/topic/5577-synergy-for-android-l-m-n-o/

    Once done, you set up the server and the android client so they can see each other, and you can successfully control the android device using your laptop. (mouse and keyboard).

    Now comes the caveat. :)

    First, the synergy client app has to be allowed to stay in background without being killed, which can usually be achieved following this guide:
    https://www.quora.com/How-do-I-prevent-Android-OS-from-closing-a-background-app-for-memory (Settings can be named slightly different, depending on your android version.)

    Once thats done - there is another issue with the android app, namely - that it sometimes looses control to the synergy server, or doesnt like to connect. ;) Luckily all those issues can be entirely solved by automating the entire connection process (timing windows), which we do next. :)

    Create the following applescript (copy paste this into ScriptEditor on a mac, and save it as an app):
    Code:
    set x to isAppLoaded("Synergy")
    if x then
        do shell script "ps aux  |  grep -i Synergy  |  awk '{print $2}'  |  xargs kill -9 & $COMMAND & $EXIT"
        do shell script "/usr/local/bin/adb disconnect"
    else
        tell application "Synergy" to activate
        tell application "Terminal"
            do script "eval $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1)"
        end tell
        delay 0.5
        do shell script "ps aux  |  grep -i Terminal  |  awk '{print $2}'  |  xargs kill -9 & $COMMAND & $EXIT"
        do shell script "/usr/local/bin/adb shell am force-stop org.synergy && /usr/local/bin/adb shell monkey -p org.synergy 1 && sleep 0.5 && /usr/local/bin/adb shell input tap 1800 480 && /usr/local/bin/adb shell input keyevent KEYCODE_APP_SWITCH && /usr/local/bin/adb shell input keyevent 19 && /usr/local/bin/adb shell input keyevent 66 && /usr/local/bin/adb disconnect"
        delay 0.5
        tell application ((path to documents folder as text) & "FireRemote:mousetoandroid.app") to activate
    end if
    
    on isAppLoaded(app_name)
        tell application "System Events"
            set app_list to every application process whose name contains app_name
            if the (count of app_list) > 0 then
                return true
            else
                return false
            end if
        end tell
    end isAppLoaded
    prerequisites:
    - adb needs to be installed (see 'getting started' on the first page of this thread) and you have to have a recent entry in your terminal history where you adb connect 'ed to the ip address of your android device (ip gets looked up by the script based on the most recent connection made).

    The script does the following.

    It looks if Synergy on the Mac is running and if so kills it (and disconnects adb). This is for exiting it, when running it a second time.

    If Synergy is not running, it starts Synergy (server), connects to the android device via adb, force closes the Synergy android client - to get a clean state on that, then launches the Synergy android client and clicks on the connect button (because the client doesnt auto connect).

    In the script above, thats the /usr/local/bin/adb shell input tap 1800 480 part. 1800 and 480 are the x and y coordinates the script clicks on, which are correct for a 1080p output (1920x1080). If your android device outputs a different screen resolution, you have to change the coordinates.

    After doing that the script launches the app switch menu on the android device (recent apps), and then presses "up" (keyevent 19) and "enter" (keyevent 66), thereby getting back to the app that was on screen on the Android device, before the Synergy android client was launched.

    If your app switch menu doesnt switch to the previous app by pressing up and then enter (f.e. because the app cards are scrolled horizonally and not vertically), you can change those keyevent inputs, with other ones you can get from this posting:
    https://stackoverflow.com/questions/7789826/adb-shell-input-events
    Then the script disconnects adb.

    The last thing the script does is to launch an app named mousetoandroid.app - which we create as well. :) That app only consists of one line of code, namely:
    Code:
    do shell script "/usr/local/bin/MouseTools -x 1439 -y 700"
    Save this as an .app named mousetoandroid.app in your ~/Documents/FireRemote/ folder (must be that folder).

    What it does is to move the mouse cursor on the Macbook to the rightmost edge of the screen (horizontally) minus one pixel. (So horizontal screen resolution of the Macbook in my case is 1440 and the x value in the script is 1439.) The only thing that does is, that it hits the edge trigger where Synergy on the Mac (server) will hand over control to the android device. So once this app gets launched, since both Synergy server and client already are connected at this point, the focus of your mouse and keyboard switches over to the android device automatically - without you needing to move your mouse.

    Depending on how you set up Synergy - the edge where Synergy switches over might not be the right side one, so you might have to change your coordinates based on that and your laptops screen resolution.

    For some reason we need a second (helper) app for that, as it doesnt work, when MouseTools is called from the first app.

    Also - you need MouseTools for that, which you can get from: http://www.hamsoftengineering.com/codeSharing/MouseTools/MouseTools.html

    Simply put the binary in /usr/local/bin and sudo chmod +x the MouseTools file to make it executable on your system.

    Also you need to whitelist the mousetoandroid.app in your System Settings/Security/Accessibility - for MouseTools to be allowed to control your Mouse cursor.

    The entire process around mousetoandroid.app we go through, because the Synergy keyboard shortcuts on macOS dont work reliably in the current version, so we cant simply switch screen focus using a keyboard shortcut. ;)
    --

    All said and done - after you've set this up, it takes 9 seconds for your Macbook (/Linux laptop) to grab and be able to control your android device, after the script is launched. And if you (first move your mouse a little to the left (in my case, so the Mac desktop draws focus in Synergy again)) and launch it a second time, Synergy and all connections to your android box exit cleanly.

    There is one final caveat. There are no keyboard shortcuts for the home or back button, once you are controlling android. So your android device should have them accessible for your mousecursor - somehow. Luckily my android box does (by clicking on the bottom edge of the screen it pulls up software keys for back and home).

    Maybe you could set keyboard shortcuts up in android keyboard settings, but I didnt try it out, since I already had my software key overlay that provided me with those actions. (edit: Remapping could be done as indicated in this posting: https://superuser.com/questions/135...ooth-keyboards-in-android-without-pressing-fn)


    edit:

    && /usr/local/bin/adb shell input keyevent 19 && /usr/local/bin/adb shell input keyevent 66

    in the script above can be replaced with:

    && /usr/local/bin/adb shell input tap 1000 333

    which is faster, but only works on 1080p resolution output. (Instead of pressing up and then enter, it just clicks on the app to switch back directly). Also gives you less animations on screen while the script is running, which makes it look cleaner.
     
    Last edited by notimp, May 23, 2020 at 8:21 PM
Draft saved Draft deleted
Loading...

Hide similar threads Similar threads with keywords - [Release], interface, scripts