Video as a background
It’s optional, AVPlayer() accepts None or a URL, but yes AVPlayer(url)
You import AVPlayer from a different file as seen in the second code. That function gets called automatically when you either create a new AVPlayer() or call the replace_item() of the AVPlayer()
RocketBlaster05 last edited by
You are going to need the youtube_dl module to get the direct link (not a webpage). If you had local files or a direct link to the media item (video / audio) this would work (pass ‘audio’ as the second argument for AVPlayer() to work with an audio player), otherwise you may have to look into the webview route unless you figure out how to install youtube_dl
cvp last edited by cvp
I think if you have downloaded your video in Pythonista files, you could use url like
cvp last edited by
import os import ui v = ui.View() v.frame = (0,0,400,400) web = ui.WebView() v.add_subview(web) movie_path = '~/Documents/MesTests/mov_dog.mp4' mov_url = 'file://' + os.path.expanduser(movie_path) mov_url = mov_url.replace(' ','%20') web.load_url(mov_url) v.present('sheet')
@cvp My introduction is haunting me...I didn’t mean what I said. I said “I made” and then “with the help”, you guys are really kind here and it looks like I just appeared snatching some code and claiming “I made” (I still edit that post to hopefully fix the way I wrote it). Also I’m not trying to ignore you or “be that new guy”, I just wanted to contribute back for all the support here (as a thank you). I guess I deserve that for my quick jump “No here’s my AVPlayer script though”, but instead I should have made my own post to share my code as thanks now I think about it...
Also feel free to correct my grammar / code, along with using my code :) I spent nearly if not over 3 years (on and off and many variants of it) updating @JonB take on his avplayer.py (thanks @JonB). I learned from that (originally borrowed his seek method otherwise figured out how it worked) and realized not long ago I don’t actually have to define a CMTime struct in order to seek. The longest and first feat was figuring out how to rotate the video lol...and thirdly obtaining a kCMTimeZero for better precision for seeking after figuring out how to get the seek to work... Also realized since the AVPlayerLooper doesn’t allow a swapTemplateItem method, I just use the standard AVPlayer which works as intended (after a replace item otherwise the AVPlayerLooper freaks out -> requires more replacing). I made the AVPlayer class so I can reuse it, make it better, but more importantly hook it up to work with playlists (once I figure out how to implement that feature). I have lots of ideas; mute audio, add video / audio objects, look into an audio normalizer to save your ears, loopCount, loopBy, and more
cvp last edited by
@ts Don't worry, you can't find worst grammar than mine and even my code is always quick and dirty (my signature 😁). In the forum, for me, if we can help somebody with some lines, it is good. The requester can always make the code better or easier for his personal use.
Welcome here in this marvelous forum, full of marvelous guys using this marvelous app developed by a marvelous super-héro.
I entirely agree with your last statement, that is very true (still amazing to this day)
@RocketBlaster05 Oh sorry, sometimes I do that. You can take out the .isfile() check in the replace_item() to allow links (I just realized my mistake) and take out the is_int function in the first script (I took out a method that used it but fix is_number() first since that’s the only thing using it). As well take out the “import os” since there is no more, a call to that module. Also that code (first script) can be saved however you want to name it, as well as the class name, but when you want to utilize it, the format is
from file_name import something
So in my case, the file_name is AVPlayer and I’m importing the class AVPlayer. Also if you run into issues, discuss it somewhere on this forum so I can improve it
The other thing I noticed was in the init(), I did
if type(arg) == str:
if isinstance(arg, str):
(The difference as I looked it up when I first noticed it is isinstance accepts inheritance whereas type does not)
@RocketBlaster05 If you still require the youtube_dl module, you can do it as suggested here or alternatively a small edit I did from his script years ago (in order to get it to work, also, let me know if this code should be removed from this post). Every other week or so, you can backup then download to keep it updated
# coding: utf-8 # author: Shaun Hevey # youtube-dl downloader is used to download youtube_dl and patch it to work in Pythonista. # Replace function came from http://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python # Download file was adapted from Python file downloader (https://gist.github.com/89edf288a15fde45682a) import console import os import requests import shutil import tempfile import time import ui import zipfile youtubedl_dir = 'youtube_dl' youtubedl_location = './site-packages/' backup_location = './backup/youtube_dl/' youtubedl_downloadurl = 'https://yt-dl.org/downloads/latest/youtube-dl' youtubedl_unarchive_location = './temp/' files_to_change = [('utils.py','import ctypes','#import ctypes'), ('utils.py','import pipes','#import pipes'), ('YoutubeDL.py','self._err_file.isatty() and ',''), ('downloader/common.py','(\'\\r\\x1b[K\' if sys.stderr.isatty() else \'\\r\')','\'\\r\''), ('extractor/common.py',' and sys.stderr.isatty()','')] def backup_youtubedl(sender): console.show_activity('Checking for youtube-dl') if os.path.isdir(youtubedl_location+youtubedl_dir): console.show_activity('Backing up youtube-dl') if not os.path.exists(backup_location): os.makedirs(backup_location) shutil.move(youtubedl_location+youtubedl_dir,backup_location+youtubedl_dir+ time.strftime('%Y%m%d%H%M%S')) console.hide_activity() @ui.in_background def restore_youtubedl_backup(sender): if not os.path.isdir(backup_location) or not os.listdir(backup_location): console.alert('Nothing to do', 'No backups found to restore') else: folders = os.listdir(backup_location) folder = folders[len(folders)-1] shutil.move(backup_location+folder,youtubedl_location+youtubedl_dir) console.alert('Success','Successfully restored '+folder) def downloadfile(url): localFilename = url.split('/')[-1] or 'download' with open(localFilename, 'wb') as f: r = requests.get(url, stream=True) total_length = r.headers.get('content-length') if total_length: dl = 0 total_length = float(total_length) for chunk in r.iter_content(1024): dl += len(chunk) f.write(chunk) #.setprogress(dl/total_length*100.0) else: f.write(r.content) return localFilename def process_file(path): os.mkdir(youtubedl_unarchive_location) if zipfile.is_zipfile(path): zipfile.ZipFile(path).extractall(path=youtubedl_unarchive_location) @ui.in_background def update_youtubedl(sender): if os.path.exists(youtubedl_location+youtubedl_dir): msg = 'Are you sure you want to update youtubedl exists in site-packages and will be overwritten' if not console.alert('Continue',msg,'OK'): return console.show_activity('Downloading') file = downloadfile(youtubedl_downloadurl) console.show_activity('Extracting') process_file(file) console.show_activity('Moving') if os.path.exists(youtubedl_location+youtubedl_dir): shutil.rmtree(youtubedl_location+youtubedl_dir) shutil.move(youtubedl_unarchive_location+youtubedl_dir, youtubedl_location) console.show_activity('Cleaning Up Download Files') shutil.rmtree(youtubedl_unarchive_location) os.remove(file) console.show_activity('Making youtube-dl friendly') process_youtubedl_for_pythonista() console.hide_activity() def process_youtubedl_for_pythonista(): for patch in files_to_change: filename, old_str, new_str = patch replace_in_file(youtubedl_location+youtubedl_dir+'/'+filename, old_str, new_str) def replace_in_file(file_path, old_str, new_str): with open(file_path, encoding='utf-8') as old_file: #Create temp file fh, abs_path = tempfile.mkstemp() os.close(fh) # close low level and reopen high level with open(abs_path,'w', encoding='utf-8') as new_file: for line in old_file: new_file.write(line.replace(old_str, new_str)) #Remove original file os.remove(file_path) #Move new file shutil.move(abs_path, file_path) def make_button(title, action): button = ui.Button(title=title) button.action = action button.background_color ='lightgrey' button.border_color = 'black' button.border_width = 1 button.flex = 'WB' return button view = ui.View(frame=(0,0,172,132)) view.background_color = 'white' backup_button = make_button(title='Backup YoutubeDL', action=backup_youtubedl) backup_button.center = (view.width * 0.5, view.y+backup_button.height) view.add_subview(backup_button) restore_button = make_button(title='Restore YoutubeDL', action=restore_youtubedl_backup) restore_button.center = (view.width * 0.5, backup_button.y+restore_button.height*1.75) view.add_subview(restore_button) download_button = make_button(title='Download YoutubeDL', action=update_youtubedl) download_button.center = (view.width * 0.5, restore_button.y+download_button.height*1.75) view.add_subview(download_button) view.present('sheet')