ffpmeg - how to detect if video crop is completed? - node.js

Thanks in advance.
I'm trying to crop a .mp4 video using an ffmpeg binary (within the context of an electron-react-app).
(The binary is run in a child process using execFile() and outputs to a temp folder which is later deleted)
ffmpeg varies considerably in the time it takes to complete the creation of a cropped video file (1sec to 18sec) depending on the computer (mac vs Windows).
I need to read the cropped video file.
I've set up an event listener in the Main process of electron
if (!monitorCroppedFile) {
console.log(`${croppedFilePath} doesn't exist`);
} else {
console.log(`${croppedFilePath} exists !`)
...readFile...;
Once monitorCroppedFile = true I read it using fs.readfile().
The problem is that ffmpeg initally creates the cropped file path but it sometimes takes ages to complete the process of cropping.
This results in the read file often being blank (as the read is triggered on detecting the file path of the cropped file).
I've tried using -preset ultrafast in the ffmpeg arguments but this only improves things on Windows marginally.
The problem doesn't occur on Macs.
Can anybody suggest a possible solution ? Is there a way to detect when the crop is fully completed ?
Many thanks.

Add -progress FILE to your command where FILE should be a filename. ffmpeg will log processing status to that file. Search for the line progress=end in it. Once you find it, you can read the file.

Related

ffmpeg extract video frames from url to a local variable in Node

I'm using the Nodejs spawn() functionality to execute a shell command that goes like this:
ffmpeg -i https://someUrl.com/someVideo.mp4 -vf fps=30 frame-%d.bmp
Which works for me as to saving 30 frames per second into numbered files, but I'd like to save every frame into a local variable so I could parse it in whatever way I need to (specifically to a JSON).
What would be the best way to do so?
Thanks in advance.

Adding watermark to video

I am able to use the moviepy library to add a watermark to a section of video. However when I do this it is taking the watermarked segment, and creating a new file with it. I am trying to figure out if it is possible to simply splice in the edited part back into the original video, as moviepy is EXTREMELY slow writing to the disk, so the smaller the segment the better.
I was thinking maybe using shutil?
video = mp.VideoFileClip("C:\\Users\\admin\\Desktop\\Test\\demovideo.mp4").subclip(10,20)
logo = (mp.ImageClip("C:\\Users\\admin\\Desktop\\Watermark\\watermarkpic.png")
.set_duration(20)
.resize(height=20) # if you need to resize...
.margin(right=8, bottom=8, opacity=0) # (optional) logo-border padding
.set_pos(("right","bottom")))
final = mp.CompositeVideoClip([video, logo])
final.write_videofile("C:\\Users\\admin\\Desktop\\output\\demovideo(watermarked).mp4", audio = True, progress_bar = False)
Is there a way to copy the 10 second watermarked snippet back into the original video file? Or is there another library that allows me to do this?
What is slow in your use case is the fact that Moviepy needs to decode and reencode each frame of the movie. If you want speed, I believe there are ways to ask FFMPEG to copy video segments without rencoding.
So you could use ffmpeg to cut the video into 3 subclips (before.mp4/fragment.mp4/after.mp4), only process fragment.mp4, then reconcatenate all clips together with ffmpeg.
The cutting into 3 clips using ffmpeg can be done from moviepy:
https://github.com/Zulko/moviepy/blob/master/moviepy/video/io/ffmpeg_tools.py#L27
However for concatenating everything together you may need to call ffmpeg directly.

batch FFMPEG-Normalize AND convert via Python?

I am currently working on a script to help me batch convert and
normalize audio files (wma to mp3)
In the search of useful tools I was lucky to stumble on FFMPEG-Normalize!
My script is running from Python and I am calling FFMPEG via subprocess.
I could not get the FFMPEG-Normalize to output Mp3 files - thus I am
doing another FFMPEG call to convert the resulted wav files.
Do you know how to make FFMPEG normalize also convert to mp3 ?
The second issue is that only part of the files in my folder are being
processed, I cant understand why. Out of 8 files I have in the path,
sometimes all of them are processed and sometimes only 3, or 5... very
weird!
Here is my code :
for file in sorted(os.listdir(pathdes)):
os.chdir(pathdes)
subprocess.call(['ffmpeg-normalize','-m','-l','-0.1',file])
file = 'normalized-' + file
file = file[:-3] + "wav"
file2 = file[:-3] + "mp3"
os.chdir(pathdes)
subprocess.call(['ffmpeg', '-i', file,'-b:a','320k', file2])
I understand FFMPEG normalize was written in Python, maybe there is
another way to call it other than subprocess ?
Am I missing something ? (i know i am !)
Thank you so much !
The ffmpeg-normalize tool allows you to set an audio encoder as well, using the -a, --acodec <acodec> option.
For example, to EBU R128-normalize a bunch of WAV files and encode them to MP3 with libmp3lame:
ffmpeg-normalize --ebu --acodec libmp3lame --extra-options "-b:a 192k" *.wav
Note that for MP3 specifically, you could use MP3Gain to change the volume without having to re-encode the files.

How do I combine AUDIO group with VIDEO stream and produce a new .ts file using ffmpeg?

Here is the input manifest:
$ curl 'https://example.net/ipadlive/index_new.m3u8?sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=YYYY&hubid=51&zipcode='
#EXTM3U
#EXT-X-VERSION:4
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="group",NAME="eng",DEFAULT=YES,AUTOSELECT=YES,LANGUAGE="en",URI="https://example.net/ipadlive/06_new.m3u8?cdnHost=da148.cdn.iptv.example.net&sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=CHAN&hubid=51&zipcode=&countycode=null&fta=null&optimumid=null&devicename=&devicetype=0&osver=&res=&fps="
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="group",NAME="spa",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="en",URI="https://example.net/ipadlive/07_new.m3u8?cdnHost=da148.cdn.iptv.example.net&sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=CHAN&hubid=51&zipcode=&countycode=null&fta=null&optimumid=null&devicename=&devicetype=0&osver=&res=&fps="
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=479776,RESOLUTION=240x180,CODECS="avc1.42c00c,mp4a.40.2",AUDIO="group"
https://example.net/ipadlive/01_new.m3u8?cdnHost=da148.cdn.iptv.example.net&sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=CHAN&hubid=51&zipcode=&countycode=null&fta=null&optimumid=null&devicename=&devicetype=0&osver=&res=&fps=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=780576,RESOLUTION=320x240,CODECS="avc1.42c00d,mp4a.40.2",AUDIO="group"
https://example.net/ipadlive/02_new.m3u8?cdnHost=da148.cdn.iptv.example.net&sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=CHAN&hubid=51&zipcode=&countycode=null&fta=null&optimumid=null&devicename=&devicetype=0&osver=&res=&fps=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1079872,RESOLUTION=480x360,CODECS="avc1.42c01e,mp4a.40.2",AUDIO="group"
https://example.net/ipadlive/03_new.m3u8?cdnHost=da148.cdn.iptv.example.net&sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=CHAN&hubid=51&zipcode=&countycode=null&fta=null&optimumid=null&devicename=&devicetype=0&osver=&res=&fps=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1682976,RESOLUTION=640x480,CODECS="avc1.42c01e,mp4a.40.2",AUDIO="group"
https://example.net/ipadlive/04_new.m3u8?cdnHost=da148.cdn.iptv.example.net&sessionid=81893121496608402793&ipaddress=x.x.x.x&callsign=CHAN&hubid=51&zipcode=&countycode=null&fta=null&optimumid=null&devicename=&devicetype=0&osver=&res=&fps=
I've never seen this before where the audio stream is a separate url than a video stream listed in the manifest.
Is there a way I can combine an audio stream and a specific video stream to produce a new stream that has both audio and video in it?
I was doing something like this:
ffmpeg -i <manifest> -c copy test.m3u8 and I don't get any audio.
I've tried changing <manifest> to an individual video stream, but then no audio. If I change it to an AUDIO stream I get no video.
I recently had the problem of combining an audio .ts file with its accompanying video .ts file. I was able to solve it using the following method for Windows users. [see - Video resource ]
1) You will need to download the ffmpeg library that will allow Windows to combine both files together. In my case I was running Windows 8 (32 bit OS) and chose a static build:
2) I then opened notepad and wrote the following code once ffmpeg was installed:
ffmpeg -i VIDEO.ts -i AUDIO.ts -c:v copy -c:a copy OUTPUT.mp4
I saved the notepad file as "joiner.bat"
NB: this bat file must present in the same folder as your separate audio and video ts files in order to combine them!!!
3) Once the bat file is in the same folder as your audio and video ts files you can double click on the joiner.bat file to combine the audio and video ts files into a single mp4 (OUTPUT.mp4) file.
I hope this helps the more novice types among us. Yes I'm still a n00b after many years - don't worry! ;)

About Tkinter python 2.76 on Linux Mint 17.2

I have 2 functions as below:
def select_audio():
os.chdir("/home/norman/songbook")
top1.lower(root)
name=tkFileDialog.askopenfilename()
doit="play " + name
top1.lift(root)
os.system(doit)
def select_video():
os.chdir("/home/norman/Videos")
top2.lower(root)
name=tkFileDialog.askopenfilename()
doit="mpv --fs " + name
top2.lift(root)
os.system(doit)
They are selected from buttons to allow choosing and playing audio files or video files.
They work to some extent.
Videos are in a different directory and at the same level as the audio files.
It doesn't matter which I choose first I see the correct directory so I can play say a video, if after it's finished I choose audio it still shows the video directory.
Similarly if I first choose audio it still shows the audio directory if I select videos.
I have no idea why it does this. I am not an experienced programmer as you can probably tell from the code.
Some suggestions:
Use a raw string to make sure that Python doesn't try to interpret anything following a \ as an escape sequence:
Change os.chdir("/home/norman/whatever") to os.chdir(r"/home/norman/whatever")
It won't solve this problem, but it will avoid you future problems.
For tkFileDialog use the initialdir option:
Change name=tkFileDialog.askopenfilename() to
name=tkFileDialog.askopenfilename(initialdir=r"home/norman/whatever", parent=root)

Resources