Add flv file merger #1012

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
4 participants
Contributor

thesues commented Jul 9, 2013

some website,such as www.youku.com, usually splits large video into several parts. So we need to merge all the parts after finishing the download.

add new option "-m"/"--merge-video" to merge flv/f4v videos.

@jaimeMF jaimeMF commented on the diff Jul 9, 2013

youtube_dl/PostProcessor.py
@@ -231,3 +231,22 @@ def run(self, information):
information['format'] = self._preferedformat
information['ext'] = self._preferedformat
return False,information
+
+
+class FlvFileMerge(PostProcessor):
+ def __init__(self):
+ self.input_files = []
+ def run(self, information):
+ self.input_files.append(information.get('filepath').encode('utf8'))
+ file_format = information.get('format')
+ if information.get('last') and (file_format == "flv" or file_format == "f4v"):
+ try:
+ merge_title = information.get('title').encode('utf8') + "-MERGED.flv"
@jaimeMF

jaimeMF Jul 9, 2013

Collaborator

You have to use filepath, because the destination path can be anywhere in the filesystem, if you use title it will end in the current directory.

Collaborator

jaimeMF commented Jul 9, 2013

Thanks for your contribution, I had posted a similar PR to solve this issue (#995)

Some comments:

  • You can use ffmpeg to merge the video parts (this works for all video formats), see my PR for see how it works
  • youtube-dl is released in the public domain, all the source code must be in public domain
  • Parts of flvconcat.py will not work in python3, at least the print foo should be converted to print(foo)

So I would try to remove the flvconcat.py file and use ffmpeg instead.

@phihag phihag commented on the diff Jul 29, 2013

youtube_dl/flvconcat.py
@@ -0,0 +1,489 @@
+#!/bin/env python
+#-*- coding: utf-8 -*-
+
+
+#use code from https://github.com/shinohane/FLVCopyCat
+#http://www.apache.org/licenses/LICENSE-2.0
@phihag

phihag Jul 29, 2013

Collaborator

This license is not compatible with youtube-dl's Unlicense

hello @jaimeMF @thesues,
i do not belong to technical profession still
regarding issue #1694
i tried using ffmpeg but it does not work...it creates a new file but use only the first file, rest of the parts of the video are ignored...
also will this postprocessor.py work on mp4 videos or its just for flv...

puneet1984 referenced this pull request Nov 22, 2013

Closed

feature request #1804

#!/usr/bin/env python

import os.path
import subprocess

def get_usable_ffmpeg(cmd):
try:
p = subprocess.Popen([cmd, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
vers = str(out, 'utf-8').split('\n')[0].split(' ')
assert (vers[0] == 'ffmpeg' and vers[2][0] > '0') or (vers[0] == 'avconv')
return cmd
except:
return None

FFMPEG = get_usable_ffmpeg('ffmpeg') or get_usable_ffmpeg('avconv')

def has_ffmpeg_installed():
return FFMPEG is not None

def ffmpeg_convert_ts_to_mkv(files, output = 'output.mkv'):
for file in files:
if os.path.isfile(file):
params = [FFMPEG, '-i']
params.append(file)
params.append(output)
subprocess.call(params)

return

def ffmpeg_concat_mp4_to_mpg(files, output = 'output.mpg'):
for file in files:
if os.path.isfile(file):
params = [FFMPEG, '-i']
params.append(file)
params.append(file + '.mpg')
subprocess.call(params)

inputs = [open(file + '.mpg', 'rb') for file in files]
with open(output + '.mpg', 'wb') as o:
    for input in inputs:
        o.write(input.read())

params = [FFMPEG, '-i']
params.append(output + '.mpg')
params += ['-vcodec', 'copy', '-acodec', 'copy']
params.append(output)
subprocess.call(params)

for file in files:
    os.remove(file + '.mpg')
os.remove(output + '.mpg')

return

def ffmpeg_concat_ts_to_mkv(files, output = 'output.mkv'):
params = [FFMPEG, '-isync', '-i']
params.append('concat:')
for file in files:
if os.path.isfile(file):
params[-1] += file + '|'
params += ['-f', 'matroska', '-c', 'copy', output]

try:
    if subprocess.call(params) == 0:
        return True
    else:
        return False
except:
    return False

def ffmpeg_concat_flv_to_mp4(files, output = 'output.mp4'):
for file in files:
if os.path.isfile(file):
params = [FFMPEG, '-i']
params.append(file)
params += ['-map', '0', '-c', 'copy', '-f', 'mpegts', '-bsf:v', 'h264_mp4toannexb']
params.append(file + '.ts')

        subprocess.call(params)

params = [FFMPEG, '-i']
params.append('concat:')
for file in files:
    f = file + '.ts'
    if os.path.isfile(f):
        params[-1] += f + '|'
if FFMPEG == 'avconv':
    params += ['-c', 'copy', output]
else:
    params += ['-c', 'copy', '-absf', 'aac_adtstoasc', output]

if subprocess.call(params) == 0:
    for file in files:
        os.remove(file + '.ts')
    return True
else:
    raise

def ffmpeg_concat_mp4_to_mp4(files, output = 'output.mp4'):
for file in files:
if os.path.isfile(file):
params = [FFMPEG, '-i']
params.append(file)
params += ['-c', 'copy', '-f', 'mpegts', '-bsf:v', 'h264_mp4toannexb']
params.append(file + '.ts')

        subprocess.call(params)

params = [FFMPEG, '-i']
params.append('concat:')
for file in files:
    f = file + '.ts'
    if os.path.isfile(f):
        params[-1] += f + '|'
if FFMPEG == 'avconv':
    params += ['-c', 'copy', output]
else:
    params += ['-c', 'copy', '-absf', 'aac_adtstoasc', output]

if subprocess.call(params) == 0:
    for file in files:
        os.remove(file + '.ts')
    return True
else:
    raise

problem is it has a MIT licence as pointed out by @jaimeMF in #1804 but it is not mentioned anywhere in the script...

Collaborator

jaimeMF commented Nov 22, 2013

@puneet1984 the license is clearly stated in the README, it doesn't need to be mentioned in every single file of the project.
I did a similar thing using ffmepg in the PR #995 that supports mp4 and flv videos, but it hasn't been merged yet.

thesues closed this Oct 30, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment