I'm working on a python script that does a custom conversion of videos via ffmpeg.
My problem is that the execution of ffmpeg stops suddenly after a bit of conversion (usually 3-4 mb out of 100mb) with a None exit code.
I'm using the pexpect library. Currently I do not check the progress but I will in a close future. It seems that I am right using pexpect regarding these questions FFMPEG and Pythons subprocess and Getting realtime output from ffmpeg to be used in progress bar (PyQt4, stdout). This is the command that I'm running (I have checked that it's exactly this one)
nice ffmpeg -i '/full/path' -s 640x360 -strict experimental -vcodec libx264
-f mp4 - coder 0 -bf 0 -refs 1 -flags2 -wpred-dct8x8 -level 30 -crf 26
-bufsize 4000k -maxrate 350k -preset medium -acodec libvo_aacenc
-ar 48000.0 -ab 128K -threads 2 -y '/full/path/out'
I am using nice but I have tried also without it and the result ends up being the same.
I'm running pexpect this way:
output, exit = pexpect.run(self.command(), withexitstatus=True,\
logfile=logfile)
print output
print exit
Of course I have tried the same command on the command line and it works fine.
Any clue on what might be happening?
The problem ended up being a bug in pexpect run function regarding the timeout. I have found the bug that was reported before me (yes, I forget to check ;))
http://sourceforge.net/tracker/?func=detail&aid=3316509&group_id=59762&atid=492077
Sadly the bug is really old and it explains how to solve the little bug. As a workaround I could just rewrite my code using spawn instead.
I don't really like the idea of using a code with trivial bugs that is still unmaintained so I have writen the code using Popen as Albert suggested.
I will wait some time to make the choice of going taking the chances with pexpect (if the code seems reliable).
For the record, here is my working code:
output = file(LOG_FILE, 'a')
args = shlex.split(self.command_video())
return subprocess.call(args, stdout=output, stderr=output)
Thanks for the help.
Related
I'm trying to convert a video between two file types, when run, nothing happens and no file is made. I've tried doing it with subprocess and os - both have the same result - nothing.
I can do the command fine through shell. I really want to be able to use this through python.
import subprocess
command = "ffmpeg -i X:/Desktop/twd.mp4 X:/Desktop/twd.mp3"
subprocess.run(command.split(),shell=True)
nothing happens and no file is made
This is very strange. Absolutely no text printed on console? I agree #Rotem's suggestion, but at the minimum the version info should print on your console (assuming you are using one of prebuilt binaries). BTW, shell=True is not needed and not recommended.
If you wish, you can give my ffmpegio package a try, it might make your life a bit easier.
import ffmpegio
ffmpegio.transcode('X:/Desktop/twd.mp4','X:/Desktop/twd.mp3')
It should auto detect your ffmpeg binaries and run your example above.
I'm using f2py to interface between python and a fortran code. I'm using the fortran code to generate data for training a neural network, so eventually I may have to run the code and f2py up to 10 million times. Every time I use f2py it produces around 15-20 lines of output, which ends up bloating the standard output file and is possibly slowing things down.
I've tried using the "--quiet" flag mentioned in the official documentation and I've tried re-directing the output to a file or /dev/null which have reduced the amount of output but hasn't eliminated it completely.
Is there any way to suppress the output completely?
I'm using anaconda3/2019.10 and the intel compiler suite version 19.0.
The basic bash shell command I'm using is:
python -W ignore -m numpy.f2py -c -m python_rk4 --fcompiler=intel --f90flags='-g -w' --opt='-O0' --include-paths /path/to/my/data --quiet py_rk4_in.f90 2>&1 >/dev/null
The typical output looks like (this warning appears 4 times):
In file included from /software/spackages/linux-centos8-x86_64/gcc-8.3.1/anaconda3-2019.10-v5cuhr6keyz5ryxcwvv2jkzfj2gwrj4a/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h(12),
from /software/spackages/linux-centos8-x86_64/gcc-8.3.1/anaconda3-2019.10-v5cuhr6keyz5ryxcwvv2jkzfj2gwrj4a/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h(4),
from /local_scratch/pbs.131339.pbs02/tmphkx4m82n/src.linux-x86_64-3.7/fortranobject.h(13),
from /local_scratch/pbs.131339.pbs02/tmphkx4m82n/src.linux-x86_64-3.7/fortranobject.c(2):
/software/spackages/linux-centos8-x86_64/gcc-8.3.1/anaconda3-2019.10-v5cuhr6keyz5ryxcwvv2jkzfj2gwrj4a/lib/python3.7/site-packages/numpy/core/include/numpy/ndarraytypes.h(84): warning #2650: attributes ignored here
NPY_CHAR NPY_ATTR_DEPRECATE("Use NPY_STRING")
The output seems to be python warnings which the -W ignore flag should take of, but that doesn't seem to work. I'm pretty new to python, so I'm not sure if I'm doing this right. This is my first time posting, so apologies if I've broken etiquette. Thanks for the help!
I'm trying to convert one of my CCTV RTSP streams to RTMP so I can add it onto a website. I've read many articles and understand that ffmpeg for Ubuntu 18.04 is the way to go.
I my script I've added the following code into Python 3.7:
ffmpeg -i "rtsp://xx.xxx.xx.x:554/user=admin&password=123#&channel=1&stream=0.sdp" -f flv -r 25 -s 640x480 -an "rtmp://localhost:1935/uid/1/camera1"
but I get a syntax error in the quote mark at the end of the first link (rtsp link) I've searched for hours and can't see what anyone has mentioned this before. I've also reviewed the ffmpeg documentation and can't see any issues.
Please can anyone point me in the right direction?
Thanks
Chris
According to the comment, you can't do it like this, since you can't directly execute commands from Python.
The simplest way to do it is by using os.system:
import os
os.system('ffmpeg -i "rtsp://xx.xxx.xx.x:554/user=admin&password=123#&channel=1&stream=0.sdp" -f flv -r 25 -s 640x480 -an "rtmp://localhost:1935/uid/1/camera1"')
Be careful, you either have to use single quotation marks as I did, or to escape double quotation marks.
A cleaner way to do it may use subprocess, to redirect stderr and stdout into Python variables for instance.
I am having an issue with ffmpeg stopping the encoding process, and searching on the internet has gotten me no working solutions. I am calling ffmpeg in Linux through Python's subprocess module, as so:
mergeFiles = subprocess.Popen("ffmpeg -i /home/pi/Video/video.mov -i /home/pi/Audio/test.wav -acodec copy -vcodec copy -map 0:v -map 1:a -shortest /home/pi/Final/output.mkv", shell=True)
The command prompt is waiting for me to manually end the encoding process with "ctrl-c", but I won't have access to a keyboard to kill the encoding. I just want it to stop when it's done. I have even attempted to use mergeFiles.kill() from Python after a couple seconds, and that doesn't even work. Help!
EDIT: If I wasn't clear, I meant that there is no error, ffmpeg simply won't continue until I hit "ctrl-c". I just want it to stop encoding when it's done. This is what my command prompt looks like:
It's just waiting for me to press "ctrl-c"
When the file /home/pi/Final/output.mkv already exists, ffmpeg asks you to override the file and does nothing.
You can pass '-y' flag to overwrite.
I am attempting to call ffmpeg to create an image from a frame in a video, I am using python to do this with subprocess.Popen on a mac, eventually this will move to a unix server.
I can successfully create a video from the command line with this line
ffmpeg -i /Users/bimemployee/Movies/ski\ commute.m4v -r .5 -vframes 1 -ss 00:01:14 /Users/bimemployee/Movies/untitled\ folder/image-%d.jpeg
I then turn this into a python iterable and passed it Popen
s=["ffmpeg","-i","Users/bimemployee/Movies/ski\ commute.m4v","-r","1","-vframes","1","-ss","00:01:14","/Users/bimemployee/Movies/untitled\ folder/image-%d.jpeg"]
subprocess.Popen(s)
When I do so I get the standard info screen from ffmpeg and an error that says Users/bimemployee/Movies/ski\ commute.m4v: No such file or directory
Why would this path work ok from the command line but not from python?
Secondly is their a better library for handling this, the ones I could find don't seem to be active projects or don't work with straight python but require things like cython.
Thanks,
CG
You're missing the opening forward slash:
/Users/bimemployee/Movies/ski_commute.m4v
is not the same as
Users/bimemployee/Movies/ski_commute.m4v