Windows - Python script to stack movies - problem

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
redglory Offline
Senior Member
Posts: 108
Joined: Dec 2011
Reputation: 2
Location: Portugal
Post: #1
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

HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: 30 GB SSD [SO] :: 3TB [Storage]
powered by Win7 | XBMC | SABnzbd | CouchPotato | SickBeard | Headphones

[Image: all-thin-banner.jpg]
(This post was last modified: 2013-12-08 23:18 by redglory.)
find quote
divingmule Offline
Skilled Python Coder
Posts: 1,297
Joined: Oct 2008
Reputation: 61
Post: #2
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.
find quote
redglory Offline
Senior Member
Posts: 108
Joined: Dec 2011
Reputation: 2
Location: Portugal
Post: #3
I know, XBMC does that for me.

Nevertheless, I prefer to have just one movie file.

Thanks anyway Smile

HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: 30 GB SSD [SO] :: 3TB [Storage]
powered by Win7 | XBMC | SABnzbd | CouchPotato | SickBeard | Headphones

[Image: all-thin-banner.jpg]
find quote
Bstrdsmkr Offline
Posting Freak
Posts: 802
Joined: Oct 2010
Reputation: 16
Post: #4
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
find quote
MilhouseVH Offline
Posting Freak
Posts: 2,996
Joined: Jan 2011
Reputation: 69
Post: #5
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".

Texture Cache Maintenance Utility: Preload your texture cache for optimal UI performance. Remotely manage media libraries. Purge unused artwork to free up space. Find missing media. Configurable QA check to highlight metadata issues. Aid in diagnosis of library and cache related problems.
find quote
redglory Offline
Senior Member
Posts: 108
Joined: Dec 2011
Reputation: 2
Location: Portugal
Post: #6
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

HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: 30 GB SSD [SO] :: 3TB [Storage]
powered by Win7 | XBMC | SABnzbd | CouchPotato | SickBeard | Headphones

[Image: all-thin-banner.jpg]
find quote
redglory Offline
Senior Member
Posts: 108
Joined: Dec 2011
Reputation: 2
Location: Portugal
Post: #7
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

HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: 30 GB SSD [SO] :: 3TB [Storage]
powered by Win7 | XBMC | SABnzbd | CouchPotato | SickBeard | Headphones

[Image: all-thin-banner.jpg]
find quote
redglory Offline
Senior Member
Posts: 108
Joined: Dec 2011
Reputation: 2
Location: Portugal
Post: #8
Err... response is the output from ffmpeg... my bad...

Just need to write response to logfile.

HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: 30 GB SSD [SO] :: 3TB [Storage]
powered by Win7 | XBMC | SABnzbd | CouchPotato | SickBeard | Headphones

[Image: all-thin-banner.jpg]
find quote
redglory Offline
Senior Member
Posts: 108
Joined: Dec 2011
Reputation: 2
Location: Portugal
Post: #9
EDIT: SOLVED IT!

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

HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: 30 GB SSD [SO] :: 3TB [Storage]
powered by Win7 | XBMC | SABnzbd | CouchPotato | SickBeard | Headphones

[Image: all-thin-banner.jpg]
(This post was last modified: 2013-12-27 01:34 by redglory.)
find quote