Python write to ram file when using command line, ghostscript - python

I want to run this command from python:
gs.exe -sDEVICE=jpeg -dTextAlphaBits=4 -r300 -o a.jpg a.pdf
Using ghostscript, to convert pdf to series of images. How do I use the RAM for the input and output files? Is there something like StringIO that gives you a file path?
I noticed there's a python ghostscript library, but it does not seem to give much more over the command line

You can't use RAM for the input and output file using the Ghostscript demo code, it doesn't support it. You can pipe input from stdin and out to stdout but that's it for the standard code.
You can use the Ghostscript API to feed data from any source, and you can write your own device (or co-opt the display device) to have the page buffer (which is what the input is rendered to) made available elsewhere. Provided you have enough memory to hold the entire page of course.
Doing that will require you to write code to interface with the Ghostscript shared object or DLL of course. Possibly the Python library does this, I wouldn't know not being a Python developer.
I suspect that the pointer from John Coleman is sufficient for your needs though.

Related

How to create a memory-mapped file in Python that is accessible from a called application?

Ok, I realize the title probably wasn't that clear. I'll clarify here and hope someone can help with a better title.
I'm opening a compressed file (tarball or similar) in python and reading some of the contents. One of the enclosed files is quite large (about 200GB, mostly zeros). Since the python tarfile module gives me file-handle like objects, I can generally use them as if I opened the file in the archive with out ever fully decompressing the enclosed file.
Unfortunately, I have to do some processing on this enclosed file using a 3rd party tool that I can't modify. This 3rd party tool only operates on files that are on disk. It won't take input from stdin.
What I do now is extract the entire 200 GB (mostly zeros) file to the disk for further processing. Obviously, this takes a while.
What I'd like to do is (using python if possible) make a "file" on disk that maps back to the "file handle" from the tarfile module in python. I could then pass this "file" to my 3rd party tool and go from there.
My target OS is linux (though a solution that also works on OSX would be nice). I don't care about working on Windows.
Edits
External tool takes a filename (or full path) as a parameter. It prints out data to stdout (which python reads)
I've gotten a setup using sparse files working. While not as fast as I had hoped, it is significantly faster than it was before.

Using FLAC decoder and LAME Encoder to convert FLAC files using Python

I am currently attempting to undergo a project to automate my music library cataloging using Python scripts, and I desperately need help before I go insane. For starters, I am using Linux (Arch Linux to be specific). To explain, I recently began using Morituri to rip my CD collection. I especially like the Mortituri script because it checks the files three times over, and generates FLAC files immediately.
This above system works fine, but I ran into another problem. I like FLAC files for my PC, but for mobile usage, I need MP3 files. I came across this famous little bash script:
for f in *.flac; do flac -cd "$f" | lame -b 320 - "${f%.*}".mp3; done
I saw this great little script, I knew there must be some way to replicate this in python. So I began working on a script that converts FLAC to MP3, and uses the basic arguments used by their respecitve CLI interfaces. I decided it be best to start with a single file at a time, and work my way up.After learning a bit about the subprocess module and pipes in python, I wrote the code below:
#!/usr/bin/python3
from subprocess import *
MyTestFile = ('/path/to/file.flac')
def Convert_It(File):
with open(File, 'r') as infile:
FlacDecode = Popen(["flac", "-cd","-","-"],stdin=infile,stdout=PIPE)
LameEncode = Popen(["lame", "-b", "192","-", "/a/differnet/path/test.mp3"],stdin=PIPE)
Convert_It(MyTestFile)
As it turns out, the output of this script is as follows:
flac 1.3.1, Copyright (C) 2000-2009 Josh Coalson, 2011-2014 Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. Type `flac' for details.
-: ERROR while decoding metadata
state = FLAC__STREAM_DECODER_END_OF_STREAM
Warning: unsupported audio format
So I have a few questions:
Why is FLAC acting this way? In testing the script, I set to path to a real FLAC file. Why it say this?
How can I make this work?!
Really, any method using simple commands. Programs likes ffmpeg are not very easy to understand (how to set LAME q value to 0? No idea.) And other python scripts are way to long (like flac2all) for me to reverse engineer.
In review, any help is accepted. Just as long as it follows my specifications of: 1. only using the bare FLAC and LAME tools for linux (no sox or ffmpeg), 2. A script not too long. 3. Doesn't create a temporary WAV file. 4. Can encode any Flac file to MP3. I'm not even worried about metadata. Just the file itself.
Please HELP!
EDIT 08/05/2015:
I discovered I was using pipes incorrectly. Here is the code I created that solves my problem:
FlacDecode = subprocess.Popen(["flac","-cds",MyFile,"-"],stdout=subprocess.PIPE)
LameEncode = subprocess.Popen(['lame','-b','320','-'(NewFilePath)],stdin=FlacDecode.stdout,stdout=subprocess.PIPE)
EndLame = LameEncode.communicate()[0]
I had a similar problem, converting flac files to mp3, and found a one line bash solution here which reads:
for f in *.flac; do ffmpeg -i "$f" -aq 1 "${f%flac}mp3"; done
I appreciate that this doesn't answer the question but it is an elegant solution to your problem.

Python run binary and intercept file writes (using subprocess)

I have a simple command-line utility which produces output both on the console and the filesystem. While I know very well how to capture the console output, I am not aware how can I also intercept the file - for which I know the filename in advance.
I would like to keep the execution "in memory" without touching the filesystem as I immediately parse and delete the file created and this creates an unnecessary bottleneck (especially when I need to run the little tool millions of times).
So, to sum up, I am trying to achieve following:
Run a binary using python's subprocess
Capture both the tool's output AND contents of a file it creates (in current working directory with in-advance known name)
Ideally, run it all without touching the filesystem.
Since you only need to support Linux, one possibility is to use named pipes. The idea is to pre-create the output file as a named pipe, and have your process read the tool's output from the pipe.
See, for example, Introduction to Named Pipes.
The Python API is os.mkfifo().

Changing mp4ize.py to work on Windows

Mp4ize (python) is a utility for converting video files to mp4 for use on iPhone and iPod. I'm trying to get it to run on Windows.
The python script relies on the library fcntl, and according to another question ( fcntl substitute on Windows ), the Windows equivalent is win32api. The other question also says:
If you provide more details about the fcntl calls people can find windows equivalents.
and since I've had no luck trying to rewrite the code myself, I thought I'd ask here.
How can I rewrite the following code for use on Windows?
fcntl.fcntl(
p.stderr.fileno(),
fcntl.F_SETFL,
fcntl.fcntl(p.stderr.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK,
)
See here for the full source code.
This command sets the NONBLOCK option of the standard error file descriptor. This lets it pass data through before the entirety of the data has been written to it.
The patch at http://pastebin.com/Zr5LN8Ui will work, with progress indicators, on Windows. However, it will sometimes report a bad encode even when the encode was good.
It uses the solution from Non-blocking read on a subprocess.PIPE in python to allow non blocking IO, and fixes the pad option (your version didn't work for my test file) and progress bar for a modern FFMpeg.
Note that it is hardcoded to use the linked method when FFMpeg gets passed 3 or more command line options, as it messes up the first call to FFMpeg which gets the resolution of the input file.

Python Audio Edit

I am searching for a way to write a simple python
program to perform an automatic edit on an audio file.
I wrote with PIL automatic picture resizing to a predefined size.
I would like to write the same for automatic file re-encoding into a predefined bitrate.
similarly, i would like to write a python program that can stretch an audio file and re-encode it.
do i have to parse MP3's by myself, or is there a library that can be used for this?
Rather than doing this natively in Python, I strongly recommend leaving the heavy lifting up to FFMPEG, by executing it from your script.
It can chop, encode, and decode just about anything you throw at it. You can find a list of common parameters here: http://howto-pages.org/ffmpeg/
This way, you can leave your Python program to figure out the logic of what you want to cut and where, and not spend a decade writing code to deal with all of the audio formats available.
If you don't like the idea of directly executing it, there is also a Python wrapper available for FFMPEG.
There is pydub. It's an easy to use library.

Categories

Resources