ffmpeg won't stop encoding, even with -shortest command - python

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.

Related

FFMpeg concatenation with large list of inputs -- how to minimize command line length

I have a python tool that concatentates several mp4s together into a single movie file using FFMPEG run from a subprocess call. The issue is that an arbitrarily large number of inputs in the command string will hit the windows CLI character max.
ffmpeg -y -i a.mp4 -i b.mp4 -i c.mp4 -f lavfi -t 0.1 -i anullsrc -filter_complex '[0]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2: (oh- ih)/2,setsar=1[0:v];[1]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(owiw)/2:(oh-ih)/2,setsar=1[1:v];[2]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow- iw)/2:(oh-ih)/2,setsar=1[2:v]; [0:v][3:a][1:v][3:a][2:v][3:a]concat=n=3:v=1:a=1[v][a]' -c:v libx264 -map [v] -map [a] output.mp4
Assuming 3 input movies that may or may have audio (hence the null audio source).
This works fine assuming there are no more than 30-50 input files. Often the input files have rather long filepaths and can get long very quickly.
After doing a bit of reading I tried a simple:
ffmpeg -f concat -i files.txt -c copy output.mp4
Which works if I can guarantee that all the files are the same codec/resolution/etc, but this cannot be guaranteed.
I'm not clear on how to translate my filter_complex to work with a text file of inputs. Finding a specific enough solution has proven elusive.
I'd be perfectly fine assigning the input paths to a temporary environment variable but in my use case this doesn't seem to work either. The subprocess call doesn't seem to translate wildcard %MYVAR% calls to environment variables.
I feel like there has to be an answer to this problem. If anyone can suggest a solution I would greatly appreciate it.
Thanks!

ffmpeg RTSP to RTMP - Syntax error when running in Python 3

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.

Python script to type something in terminal

I'm trying to make a python script that opens a separate terminal window and immediately enters a command without the user having to type anything.
I use os.system("gnome-terminal") to open the second terminal but I have no clue how to make it go ahead an enter a command. I tried os.system("gnome-terminal -e 'python ./example.py'") but it doesn't even open a second terminal, but while I have os.system("gnome-terminal") it opens one fine.
Thanks
you can try a few ways
such as:
os.system("gnome-terminal -e 'bash -c \"sudo apt-get update; exec bash\"'")
Although on windows i opt for a sub-process, heres an example from stack:
import subprocess as sub
sub.Popen('cmd /K dir')
sub.Popen(['cmd', '/K', 'dir'])
And replace dir with whichever command you wish to use. The /k is used to keep the commandline open and run the command.
here is some other links that answer the question fairly well most of the answers actually being valid, stackoverflow

pexpect.run() terminates before ending ffmpeg without finishing the conversion

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.

Python and ffmpeg

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

Categories

Resources