Kodi Community Forum

Full Version: Python script to stack movies - problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I've been developing a little script to stack my movies:

Code:
input: movie.cd1.avi | movie.cd2.avi | ...
output: movie.avi

My problem is that when I call ffmpeg to concat part movies with Popen, and when process finishes, I have to CTRL+C to return to cmd prompt.

This is my script: stackit.py

It searches movies folder and builds an dictionary like ['movie.a',('movie.a.cd1.avi','movie.a.cd2.avi','movie.a.cd3.avi')]
for every movie folder with part movie files.

Movie folder structure:
Code:
C:\Movies
----Movie.A
------Movie.A.CD1.avi
------Movie.A.CD1.avi

It then writes each movie parts to <movie>.txt to feed ffmpeg concat command.

My problem is that it only runs successfully with Popen(ffmpeg_command) but it won't return to cmd prompt. It just hangs.

I can't run it with
Code:
call(ffmpeg_command)
or
Code:
check_output(ffmpeg_command)

If I try to get output/erros like
Code:
ffmpeg = Popen(ffmpeg_command, stdout=STDOUT, stderr=STDOUT)
(out, err) = ffmpeg.communicate()
it also fails.

With these command it always fails with
Code:
e:\Test\1941.(1979).txt: Invalid data found when processing input


It seems it fails to read my txt, perhaps because of single backslash?

Can anyone help me with this?

Thanks
I don't really have any help, just an FYI, you can create a stack:// url to have your files played concurrently.

e.g.
Code:
stack://path/to/movie.cd1.avi , path/to/movie.cd2.avi
and so on.
I know, XBMC does that for me.

Nevertheless, I prefer to have just one movie file.

Thanks anyway Smile
Not sure if this is the problem, but worth a shot. You have a function named "process" in your script. I assume that somewhere within the bowels of Popen there is a function called "process," which could be overwritten by your function. Other than that, you'll probably have better luck at StackOverflow or the ffmpeg forums
HTH
Just debugged this, sorry for not finding the time earlier.

Your first problem is that you're writing out movie.txt, but you're not flushing the file buffers so that when you call ffmpeg immediately after the file is written and closed, ffmpeg (as a separate process) is not able to read the content of the yet to be flushed file, so it gets an empty input file (you can see this situation if you call a dummy ffmpeg.cmd script in place of ffmpeg.exe, which tries to view the contents of movie.txt - you'll see the file is empty at this point). Add an f.flush() before the f.close().

Once that is resolved, you should be able to replace the call to POpen() with:
Code:
response = subprocess.check_output(ffmpeg_command, stderr=subprocess.STDOUT).decode("utf-8")

Also change the "from subprocess import *" to "import subprocess".
Man, you real are a Python guru!

Just nailed it as expected!

Now i'm just going to log ffmpeg output to logfile.

thanks a lot it was driving me crazy! Smile
Just wondering why Popen without any arguments opens file and when defining STDOUT and STDERR with communicate() won't open unflushed file. Just out of curiosity.

Without STDOUT defined i can't log ffmpeg output, only errors. I suppose i can use check_call() otherwise.

I'm going to stack my movies now Smile
Err... response is the output from ffmpeg... my bad...

Just need to write response to logfile.
EDIT: SOLVED IT!

Had to replace my moviepath like this:
Code:
moviefile = moviepart.replace("'", r"'\''")