How to run this program, dump_video.py? - python

I want to run this program, http://pymedia.org/tut/src/dump_video.py.html
It converts video file to image files. I've installed all the modules. When I execute it by run in Python IDLE, it prints Usage... same stuff at the end of the program. My video file is in .avi, xvid codec, says it supports it on page pymedia. I believe that program and my file arent connected, but how to input my file (test.avi) to the program? I put the video file in same folder as program. Says something at the end of the page http://pymedia.org/tut/index.html, to put in cmd, and I did but i keep getting the same message about Usage. Its in the if statement at the end of the file. I worked a little in python, but never with functions, so please help.
Thanks!

The source code contains this Usage message:
print 'Usage: dump_video <file_name> <image_pattern> <format_number>\n<format_number> can be: RGB= 2'+\
'\n<image_patter> should include %d in the name. ex. test_%d.bmp.'+ \
'\nThe resulting image will be in a bmp format'
Per the comments, dump_video.py can be called this way:
dump_video.py myvideo.avi myvideo_%d.bmp 2
This will attempt to save the frames in myvideo.avi into BMP files of the form myvideo_%d.bpm where %d will be replaced by numbers. I'm not sure what the last argument, the so-called "format number", 2 does.

Related

Take the path of a file by dropping it on batch or python

in my head this problem seems simple but I cant for the life of me figure it out.
I want to use a function similar to os.replace() to move a file/folder from one location which could vary to one that is set whilst also preserving the name of it.
At this point I couldn't figure it out however to make it slightly more difficult I want to be able to drop a file onto the batch/python script and have the code detect the filepath for the file i dropped on it.
Sorry for the bad explanation in short:
import os
initialfilepath = "The filepath of the file i drop onto the batch/python file"
finalfilepath = "Predetermined/file/path etc"
os.replace(initialfilepath,finalfilepath) <--However i want to preserve the name of the file.
Any help would be greatly appreciated!
With a single line in batch file you can handle all your dropped files:
CMD /k FOR %%s in (%*) do ECHO %%s
In this example it will print all dropped files.
The parameter %* get all file pathnames.
The command FOR as it is shown, split the string by spaces, and handle one by one.
If the file pathname has spaces, so it will come in quotes as one thing.
Note that you don't need CMD /k, but it will keep the console opened at the end.
If you just wanna see before to close, insert PAUSE > nul at the end.
You can group the do commands between ( ... ) and then break the lines.
Consider set #ECHO off in beggining.
Now try it yourself :)
Type FOR /? to see more possibilities to manipulate parameters %~nX, %~dpX, %~xX...

How to retrieve the unsaved output of a statement in python?

I ran this line to retrieve the paths of all the files in a directory and its subdirectories and forgot to save it in a variable and it takes hours to run it again because of the size of the dataset.
list(glob.glob(str(train_root)+'/**/info.txt',recursive=True))
Is it saved in a variable name reserved by python like temp?
then I can run my_list=temp.
If you already ran the code, python released the memory after the code finished executing. You will likely have to rerun it to get the output.
If you ran this in IDLE, then you can certainly save to a file with something like:
f = open('<output_file_name>','w') and then print(<string_of_data>, file=f) or f.write(<string_of_data>). Finish off with f.close()
Running that will save an output text file containing that variable. You may need to cast the array as a string and specify a path for the output file.

How do you use Python Ghostscript's high-level interface to convert a .pdf file into multiple .png files?

I am trying to convert a .pdf file into several .png files using Ghostscript in Python. The other answers on here were pretty old hence this new thread.
The following code was given as an example on pypi.org of the 'high level' interface, and I am trying to model my code after the example code below.
import sys
import locale
import ghostscript
args = [
"ps2pdf", # actual value doesn't matter
"-dNOPAUSE", "-dBATCH", "-dSAFER",
"-sDEVICE=pdfwrite",
"-sOutputFile=" + sys.argv[1],
"-c", ".setpdfwrite",
"-f", sys.argv[2]
]
# arguments have to be bytes, encode them
encoding = locale.getpreferredencoding()
args = [a.encode(encoding) for a in args]
ghostscript.Ghostscript(*args)
Can someone explain what this code is doing? And can it be used somehow to convert a .pdf into .png files?
I am new to this and am truly confused. Thanks so much!
That's calling Ghostscript, obviously. From the arguments it's not spawning a process, it's linked (either dynamically or statically) to the Ghostscript library.
The args are Ghostscript arguments. These are documented in the Ghostscript documentation, you can find it online here. Because it mimics the command line interface, where the first argument is the calling program, the first argument here is meaningless and can be anything you want (as the comment says).
The next three arguments turn on SAFER (which prevents some potentially dangerous operations and is, now, the default anyway), sets NOPAUSE so the entire input is processed without pausing between pages, and BATCH so that on completion Ghostscript exits instead of returning to the interactive prompt.
Then it selects a device. In Ghostscript (due to the PostScript language) devices are what actually output stuff. In this case the device selected is the pdfwrite device, which outputs PDF.
Then there's the OutputFile, you can probably guess that this is the name (and path) of the file where the output is to be written.
The next 3 arguments; -c .setpdfwrite -f are, frankly archaic and pointless. They were once recommended when using the pdfwrite device (and only the pdfwrite device) but they have no useful effect these days.
The very last argument is, of course, the input file.
Certainly you can use Ghostscript to render PDF files to PNG. You want to use one of the PNG devices, there are several depending on what colour depth you want to support. Unless you have some stranger requirement, just use png16m. If your input file contains more than one page you'll want to set the OutputFile to use %d so that it writes one file per page.
More details on all of this can, of course, be found in the documentation.

What is the correct form of arguments to Python subprocess()

I need to convert an existing short wav file to mp3 (wma would also be acceptable).
I have installed ffmpeg in the C: directory as recommended, and it works when used directly from a Windows command line, but not in Python 2.7
At first I used os.system() but although the code doesn't create an error, it does not create the mp3 file either
Using the preferred subprocess.call() gives an error message.
This one of several attempts to make it work:
import os,sys
import subprocess
# .wav file exists and contains 1 minute of audio
os.system('ffmpeg -i G:/Channel1_08.wav G:/Channel1_08.mp3')
# line above executes but does not create an mp3 file
# the same command works in a Windows command line
# ffmpeg is in the C: directory
## subprocess is supposed to be better than os.system, I tried
s = subprocess.call("ffmpeg -i G:/Channel1_08.wav G:/Channel1_08.mp3",shell = True)
# line above returns s=1, but no mp3 file
Edit: more info, I'm using a Win8.1 PC, the files are on a USB stick.
with other variations of the argument string, I often get "file not found" errors, but the file is definitely in place.
It occurs to me that I have not re-booted the PC since changing the PATH variable. I cannot do this until a major search script has finished running in about 24 hours time. I'll keep you posted.
14 Sep 17: Rebooting fixed the problem. For the record the original wav file was 2113KB, the wma is 1101KB and the mp3 is 199KB.

File names have a `hidden' m character prepended

I have a simple python script which produces some data in a Neutron star mode. I use it to automate file names so I don't later forget the inputs. The script succesfully saves the file as
some_parameters.txt
but when I then list the files in terminal I see
msome_parameters.txt
The file name without the "m" is still valid and trying to call the file with the m returns
$ ls m*
No such file or directory
So I think the "m" has some special meaning of which numerous google searches do not yields answers. While I can carry on without worrying, I would like to know the cause. Here is how I create the file in python
# chi,epsI etc are all floats. Make a string for the file name
file_name = "chi_%s_epsI_%s_epsA_%s_omega0_%s_eta_%s.txt" % (chi,epsI,epsA,omega0,eta)
# a.out is the compiled c file which outputs data
os.system("./a.out > %s" % (file_name) )
Any advise would be much appreciated, usually I can find the answer already posted in the stackoverflow but this time I'm really confused.
You have a file with some special characters in the name which is confusing the terminal output. What happens if you do ls -l or (if possible) use a graphical file manager - basically, find a different way of listing the files so you can see what's going on. Another possibility would be to do ls > some_other_filename and then look at the file with a hex editor.

Categories

Resources