Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify video.py #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/echonest/remix/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, sequence=None, settings=None):
def _init(self):
"extra init settings/options (can override...)"
return

def __len__(self):
"how many frames are in this sequence?"
return len(self.files)
Expand Down Expand Up @@ -100,10 +100,10 @@ def renderframe(self, index, dest=None, replacefileinseq=True):
#os.symlink(self.files[index], dest)
if replacefileinseq:
self.files[index] = dest

def render(self, direc=None, pre="image", replacefiles=True):
"renders sequence to stills. can update sequence with rendered images (default)"
if direc is None:
if direc is None:
#nothing to render...
return
dest = None
Expand Down Expand Up @@ -175,18 +175,18 @@ def __getitem__(self, index):
else:
log.warning("Frame-based sampling not supported for synchronized AV")
return None

def getslice(self, index):
return SynchronizedAV(audio=self.audio[index], video=self.video[index])

def save(self, filename):
audio_filename = filename + '.wav'
audioout = self.audio.encode(audio_filename, mp3=False)
self.video.render()
res = sequencetomovie(filename, self.video, audioout)
os.remove(audio_filename)
return res

def saveAsBundle(self, outdir):
videodir = os.path.join(outdir, "video")
videofile = os.path.join(outdir, "source.flv")
Expand All @@ -201,7 +201,7 @@ def saveAsBundle(self, outdir):


def loadav(videofile, verbose=True):
foo, audio_file = tempfile.mkstemp(".mp3")
foo, audio_file = tempfile.mkstemp(".mp3")
cmd = "en-ffmpeg -y -i \"" + videofile + "\" " + audio_file
if verbose:
log.info(cmd)
Expand Down Expand Up @@ -230,7 +230,7 @@ def loadavfrombundle(dir):
def loadavfromyoutube(url, verbose=True):
"""returns an editable sequence from a youtube video"""
#todo: cache youtube videos?
foo, yt_file = tempfile.mkstemp()
foo, yt_file = tempfile.mkstemp()
# https://github.com/rg3/youtube-dl/
cmd = "youtube-dl -o " + "temp.video" + " " + url
if verbose:
Expand All @@ -248,7 +248,7 @@ def loadavfromyoutube(url, verbose=True):

def youtubedl(url, verbose=True):
"""downloads a video from youtube and returns the file object"""
foo, yt_file = tempfile.mkstemp()
foo, yt_file = tempfile.mkstemp()
# https://github.com/rg3/youtube-dl/
cmd = "youtube-dl -o " + yt_file + " " + url
if verbose:
Expand Down Expand Up @@ -301,7 +301,7 @@ def sequencefrommov(mov, settings=None, direc=None, pre="frame-", verbose=True):
format = "jpeg"
if settings is not None:
format = settings.imageformat()
cmd = "en-ffmpeg -i " + mov + " -an -sameq " + os.path.join(direc, pre + "%06d." + format)
cmd = "en-ffmpeg -i \"" + mov + "\" -an -sameq " + os.path.join(direc, pre + "%06d." + format)
if verbose:
log.info(cmd)
out = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
Expand All @@ -315,8 +315,8 @@ def sequencefrommov(mov, settings=None, direc=None, pre="frame-", verbose=True):
def sequencetomovie(outfile, seq, audio=None, verbose=True):
"renders sequence to a movie file, perhaps with an audio track"
direc = tempfile.mkdtemp()
seq.render(direc, "image-", False)
cmd = "en-ffmpeg -y " + str(seq.settings) + " -i " + os.path.join(direc, "image-%06d." + seq.settings.imageformat())
seq.render(direc, "frame-", False)
cmd = "en-ffmpeg -y" + str(seq.settings) + " -i " + os.path.join(direc, "frame-%06d." + seq.settings.imageformat())
if audio:
cmd += " -i " + audio
cmd += " -sameq " + outfile
Expand All @@ -339,8 +339,8 @@ def convertmov(infile, outfile=None, settings=None, verbose=True):
if not isinstance(settings, VideoSettings):
raise TypeError("settings arg must be a VideoSettings object")
if outfile is None:
foo, outfile = tempfile.mkstemp(".flv")
cmd = "en-ffmpeg -y -i " + infile + " " + str(settings) + " -sameq " + outfile
foo, outfile = tempfile.mkstemp(".mp4")
cmd = "en-ffmpeg -y -i \"" + infile + "\"" + str(settings) + " -sameq \"" + outfile + "\""
if verbose:
log.info(cmd)
out = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
Expand All @@ -355,24 +355,32 @@ def settingsfromffmpeg(parsestring):
settings = VideoSettings()
parse = parsestring.split('\n')
for line in parse:
if "Stream #0.0" in line and "Video" in line:
if "Stream #" in line and "Video" in line:
segs = line.split(", ")
for seg in segs:
if re.match("\d*x\d*", seg):
#dimensions found
settings.size = map(int, seg.split(" ")[0].split('x'))
if "DAR " in seg:
#display aspect ratio
start = seg.index("DAR ")+4
if "DAR " in seg:
#display aspect ratio
start = seg.index("DAR ")+4
if "]" in seg:
end = seg.index("]", start)
settings.aspect = map(int, seg[start:end].split(":"))
else:
settings.aspect = map(int, seg[start:].split(":"))
elif re.match("(\d*\.)?\d+[\s]((fps)|(tbr)|(tbc)).*", seg):
#fps found
#todo: what's the difference between tb(r) and tb(c)?
settings.fps = float(seg.split(' ')[0])
elif re.match("\d*.*kb.*s", seg):
if "bitrate: " in line:
segs = line.split(", ")
for seg in segs:
if re.match("\d*.*kb.*s", seg):
#bitrate found. assume we want the same bitrate
settings.bitrate = int(seg[:seg.index(" ")])
start = seg.index("bitrate: ")+9
end = seg.index(" ", start)
settings.bitrate = int(seg[start:end])
return settings

def ffmpeg_error_check(parsestring):
Expand Down