d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Vlc Script
Add Reply New Topic New Poll
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
May 27 2021 09:24pm
Hi good sirs, I need a script to combine audio and video files.

Doing the merging of audio and video files by hand in VLC player is a tedious task when dealing with hundreds of pairs of audio + video.


I saved each video + audio file in their own folder.
Here's an example of what I'm dealing with:

Code
root/
audiovideo1/
videofile.ts
audiofile.aac
audiovideo2/
videofile.ts
audiofile.aac
audiovideo3/
videofile.ts
audiofile.aac


The file names are always the same; the thing that changes is the folder name containing each audio + video pair.

Ideally, I would like to combine audio + video from each folder and use the pair's folder name to create the combined file into mp4 format.
Here's the expected output based on the above example:

Code
root/
audiovideo1.mp4
audiovideo2.mp4
audiovideo3.mp4



Let me know if you can write a script to do this work please.

... or guide me into doing it :P
I think the simplest idea is to use a python (or another language) program to drive the merging script over each audiovideo folder...

This post was edited by moutonguerrier on May 27 2021 09:53pm
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
May 29 2021 05:10am
offering 2000 fg for something that works

there are example vlc scripts for batch file conversion online, but merging seems a bit more complicated

This post was edited by moutonguerrier on May 29 2021 05:12am
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
May 29 2021 08:23am
Quote (moutonguerrier @ May 29 2021 01:10pm)
offering 2000 fg for something that works

there are example vlc scripts for batch file conversion online, but merging seems a bit more complicated


does it have to use vlc? ffmpeg is made exactly for this

Code


$ cd root/
$ for i in */; do ffmpeg -i $i/videofile.ts -i $i/audiofile.aac -c:v copy -c:a aac $i.mp4 ; done

Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
May 29 2021 08:57am
Done
t4t

Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
May 31 2021 07:18am
I wanted to share the complete python script because I'm loving recursion :P
Also accepting feedback.


Code
from os import walk
import subprocess


def list_files(path):
f, d = [], []
for (_, dirnames, filenames) in walk(path):
f.extend(filenames)
d.extend(dirnames)
break
return f, d


def get_file_with_extension(files, extension):
for file in files:
if extension in file:
return file


def find_video_audio_pairs(root):
files, folders = list_files(root)

# Assumes no more than 1 video and 1 audio of the corresponding format per root folder
video_file = get_file_with_extension(files, VIDEO_FORMAT)
audio_file = get_file_with_extension(files, AUDIO_FORMAT)
if video_file and audio_file:
merge_audio_video(root + "/" + video_file, root + "/" + audio_file, root)

for f in folders:
find_video_audio_pairs(root + "/" + f)


def merge_audio_video(video, audio, output_path):
"""
Merges an audio file with a video file using ffmpeg
:param video: absolute path to input 1
:param audio: absolute path to input 2
:param output_path: absolute path to output
"""
command = subprocess.Popen(
["ffmpeg",
"-i", video,
"-i", audio,
"-c:v", "copy",
"-c:a", "aac",
output_path + MERGED_FORMAT])
command.wait()
command.kill()


if __name__ == '__main__':
VIDEO_FORMAT = ".ts"
AUDIO_FORMAT = ".aac"
MERGED_FORMAT = ".mp4"
ROOT = "/Downloads/Videos"
find_video_audio_pairs(ROOT)


This post was edited by moutonguerrier on May 31 2021 07:22am
Member
Posts: 4,689
Joined: May 30 2021
Gold: 4.00
May 31 2021 08:47am
Quote (moutonguerrier @ May 31 2021 06:18am)
I wanted to share the complete python script because I'm loving recursion :P
Also accepting feedback.


Code
from os import walk
import subprocess


def list_files(path):
f, d = [], []
for (_, dirnames, filenames) in walk(path):
f.extend(filenames)
d.extend(dirnames)
break
return f, d


def get_file_with_extension(files, extension):
for file in files:
if extension in file:
return file


def find_video_audio_pairs(root):
files, folders = list_files(root)

# Assumes no more than 1 video and 1 audio of the corresponding format per root folder
video_file = get_file_with_extension(files, VIDEO_FORMAT)
audio_file = get_file_with_extension(files, AUDIO_FORMAT)
if video_file and audio_file:
merge_audio_video(root + "/" + video_file, root + "/" + audio_file, root)

for f in folders:
find_video_audio_pairs(root + "/" + f)


def merge_audio_video(video, audio, output_path):
"""
Merges an audio file with a video file using ffmpeg
:param video: absolute path to input 1
:param audio: absolute path to input 2
:param output_path: absolute path to output
"""
command = subprocess.Popen(
["ffmpeg",
"-i", video,
"-i", audio,
"-c:v", "copy",
"-c:a", "aac",
output_path + MERGED_FORMAT])
command.wait()
command.kill()


if __name__ == '__main__':
VIDEO_FORMAT = ".ts"
AUDIO_FORMAT = ".aac"
MERGED_FORMAT = ".mp4"
ROOT = "/Downloads/Videos"
find_video_audio_pairs(ROOT)


I generally like specifying the param and return types if possible, so the signature
Code
get_file_with_extension(files, extension)


would look like
Code
get_file_with_extension(files: list, extension: str) -> str


I find that it helps when you don't have an editor handy to do that for you.
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
May 31 2021 08:49am
Quote (r4f @ May 31 2021 10:47am)
I generally like specifying the param and return types if possible, so the signature
Code
get_file_with_extension(files, extension)


would look like
Code
get_file_with_extension(files: list, extension: str) -> str


I find that it helps when you don't have an editor handy to do that for you.


That definitely makes sense to me as I'm more of a Java programmer :P
thanks
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
May 31 2021 09:36pm
shoul've called the function

search_and_merge instead of find_video_audio_pairs
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
Jun 1 2021 04:25am
You could also check for return status / or if output file is really created, and gather stats like time to convert. To make sure you're not recursing through directories and then not realize one file failed conversion due to some format issue etc

also make sure filenames are escaped correctly, try running it on folder name called

Code
Hello\ .'w orld`
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Jun 1 2021 05:15am
That folder name is mean.

Ok ill use your feedback to improve robustness! :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll