Applescript: Export quicktime audio recording - audio

With the following applescript, I try to
record the audio with "QuickTime Player" for several seconds
export the track and save it to the desktop
tell application "QuickTime Player"
launch
set doku to new audio recording
start doku
delay 4
stop doku
set savePath to "~/Desktop/record.mp3"
set newDoc to last item of (documents whose name contains "Untitled")
export newDoc in file savePath using settings preset "Audio Only"
end tell
Creating a new audio recording seems to be quite easy, but for some reason the export-command doesn't work. The error message says, that I don't have the permission to export it.

It does seem to be a sandboxing issue, but if you move the file path reference out of the QuickTime Player block and into a Finder block, it works. Also, your filename should end in “.m4a” not “.mp3” because QuickTime Player creates MP4 audio files (file extension “.m4a”) not MP3 audio files (file extension “.mp3.”)
So this version of your script works:
tell application "Finder"
set savePath to (the path to the desktop folder as text) & "record.m4a"
tell application "QuickTime Player"
activate
set doku to new audio recording
start doku
delay 4
stop doku
set newDoc to last item of (documents whose name contains "Untitled")
export newDoc in file savePath using settings preset "Audio Only"
end tell
end tell

I ran into this error as well. There is a sandboxing issue. Try saving it instead to ~/Movies/ and your code should work okay. Then you can move it around using other commands to the folder you want it to be in.

I found a solution to a related issue on the Apple Communities forum.
Instead of:
set savePath to "~/Desktop/record.mp3"
try separating the path components with a :, like:
set savePath to "Macintosh HD:Users:<your username>:Desktop:record.mp3"

Related

Path not found when creating text file

I've been through all stackoverflow posts about this and mostly its wrong path, space, wrong date format etc. etc... Nothing to help me.
I have this chunk of code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(SaveLocation & "PONUDA.TXT", True)
a.Write (txtstring)
a.Close
where second line produces that error.
SaveLocation is filled as string with various things to create folder tree and final form of it is next:
C:\Users\User\Desktop\Otpremnice\2022\January\O1-9_BuyerName\
I can confirm that folders are created and do exist, so there is no reason for code to not work. Moreover it worked a month ago when I tested and wrote it...
Were there any windows updates that broke excel or I didn't enable some references?
Excel file is stored on desktop, so is folder I create.

Xref table not zero-indexed. ID numbers for objects will be corrected. won't continue

I am trying to open a pdf to get the number of pages. I am using PyPDF2.
Here is my code:
def pdfPageReader(file_name):
try:
reader = PyPDF2.PdfReader(file_name, strict=True)
number_of_pages = len(reader.pages)
print(f"{file_name} = {number_of_pages}")
return number_of_pages
except:
return "1"
But then i run into this error:
PdfReadWarning: Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]
I tried to use strict=True and strict=False, When it is True, it displays this message, and nothing, I waited for 30minutes, but nothing happened. When it is False, it just display nothing, and that's it, just do nothing, if I press ctrl+c on the terminal (cmd, windows 10) then it cancel that open and continues (I run this in a batch of pdf files). Only 1 in the batch got this problem.
My questions are, how do I fix this, or how do I skip this, or how can I cancel this and move on with the other pdf files?
If somebody had a similar problem and it even crashed the program with this error message
File "C:\Programy\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1604, in getObject
% (indirectReference.idnum, indirectReference.generation, idnum, generation))
PyPDF2.utils.PdfReadError: Expected object ID (14 0) does not match actual (13 0); xref table not zero-indexed.
It helped me to add the strict argument equal to False for my pdf reader
pdf_reader = PdfReader(input_file, strict=False)
For anybody else who may be running into this problem, and found that strict=False didn't help, I was able to solve the problem by just re-saving a new copy of the file in Adobe Acrobat Reader. I just opened the PDF file inside an actual copy of Adobe Acrobat Reader (the plain ol' free version on Windows), did a "Save as...", and gave the file a new name. Then I ran my script again using the newly saved copy of my PDF file.
Apparently, the PDF file I was using, which were generated directly from my scanner, were somehow corrupt, even though I could open and view it just fine in Reader. Making a duplicate copy of the file via re-saving in Acrobat Reader somehow seemed to correct whatever was missing.
I had the same problem and looked for a way to skip it. I am not a programmer but looking at the documentation about warnings there is a piece of code that helps you avoid such hindrance.
Although I wouldn't recomend this as a solution, the piece of code that I used for my purpose is (just copied and pasted it from doc on link)
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
This happens to me when the file was created in a printer / scanner combo that generates PDFs. I could read in the PDF with only a warning though so I read it in, and then rewrote it as a new file. I could append that new one.
from PyPDF2 import PdfMerger, PdfReader, PdfWriter
reader = PdfReader("scanner_generated.pdf", strict=False)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open("fixedPDF.pdf", "wb") as fp:
writer.write(fp)
merger = PdfMerger()
merger.append("fixedPDF.pdf")
I had the exact same problem, and the solutions did help but didn't solve the problem completely, at least the one setting strict=False & resaving the document using Acrobat reader.
Anyway, I still got a stream error, but I was able to fix it after using an PDF online repair. I used sejda.com but please be aware that you are uploading your PDF on some website, so make sure there is nothing sensible in there.

Is there any way to set file attributes in node js

Not sure about unix, but in windows you can add attributes to files, like a location on a photo file or a duration on a video file.
Is there a way to do this in node js. Would be very handy with my currently project. It would save me having to create separate attribute data files.
You may use the WinAttr package to do that.
See this module:
https://www.npmjs.com/package/winattr
But it's for attributes like archive, hidden, readonly, system.
I don't think you can add an attribute of of duration to the video file - the duration is written in the container/codec of the video itself. The location for images is in the EXIF data - which can be manipulated with other modules on npm - see:
https://www.npmjs.com/search?q=EXIF
For the location on a photo file or a duration on a video file you need to use whatever information is appropriate for that given image or video format.
You can run a SHELL command:
var execSync = require('child_process').execSync;
// Remove Hidden and system attributes:
execSync("attrib -h -s " + yourFolder);
// Add Hidden attribute:
execSync("attrib +h " + yourFolder);

About Tkinter python 2.76 on Linux Mint 17.2

I have 2 functions as below:
def select_audio():
os.chdir("/home/norman/songbook")
top1.lower(root)
name=tkFileDialog.askopenfilename()
doit="play " + name
top1.lift(root)
os.system(doit)
def select_video():
os.chdir("/home/norman/Videos")
top2.lower(root)
name=tkFileDialog.askopenfilename()
doit="mpv --fs " + name
top2.lift(root)
os.system(doit)
They are selected from buttons to allow choosing and playing audio files or video files.
They work to some extent.
Videos are in a different directory and at the same level as the audio files.
It doesn't matter which I choose first I see the correct directory so I can play say a video, if after it's finished I choose audio it still shows the video directory.
Similarly if I first choose audio it still shows the audio directory if I select videos.
I have no idea why it does this. I am not an experienced programmer as you can probably tell from the code.
Some suggestions:
Use a raw string to make sure that Python doesn't try to interpret anything following a \ as an escape sequence:
Change os.chdir("/home/norman/whatever") to os.chdir(r"/home/norman/whatever")
It won't solve this problem, but it will avoid you future problems.
For tkFileDialog use the initialdir option:
Change name=tkFileDialog.askopenfilename() to
name=tkFileDialog.askopenfilename(initialdir=r"home/norman/whatever", parent=root)

VLCJ: Time code missing when saving a video stream from an embedded media player

I have an application that shows a video stream from a camera, and is able to save the video stream on a file on request. When I run this command from the terminal I see the video in VLC, and the contents is saved on a file as expected:
vlc v4l:///dev/video0:norm=ntsc ':sout=#duplicate{dst=display{noaudio},dst="transcode{vcodec=wmv2,vb=800}:file{dst=aaa.wmv}"}'
However, when I save a file from my application the there are no time codes in the file, so when I open the file in another application I'm unable to move backwards or forwards in the file. I can also not see how long the file is.
Here is a simplified version of my code
factory = new MediaPlayerFactory();
mainframe = new JFrame("Video Viewer");
fullscreenStrategy = new DefaultFullScreenStrategy(mainframe);
Canvas canvas = new Canvas();
canvas.setBackground(Color.black);
EmbeddedMediaPlayer player= factory.newEmbeddedMediaPlayer(fullscreenStrategy);
mainframe.add(canvas);
player.setVideoSurface(factory.newVideoSurface(canvas));
...
String media = "v4l:///dev/video0:norm=ntsc";
String filename = "aaa.wmv";
String mediaoptions = ":sout=#duplicate{ dst=display,"+
" dst=\"transcode{vcodec=wmv2,vb=800}:"+
"file{dst="+filename+"}\"}");
player.prepareMedia(media, mediaoptions);
player.start();
aaa.wmv is created, but without time codes.
What can be wrong? The only difference I see from the command line version is that I use a Canvas widget with the EmbeddedMediaPlayer instead of the native VLC view window.
Never mind, I found the problem. For the time codes to be saved properly it is necessary to call player.release(). I did that, but before the release call I copied the file to another location. Since release hadn't been called yet the file was incomplete. When I changed the code to first call player.release(), then copy the file it worked as expected.

Resources