Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
Play MPEG-CENC encrypted local file on android
26 mai 2018, par rd7773I need to decrypt and play a cenc-aes-ctr mp4 video on the fly which is locally stored on the device. The video was encrypted at server end by using following ffmpeg command and then downloaded to phone :
ffmpeg -i SampleVideo_1280x720_1mb.mp4 -vcodec copy -acodec copy -encryption_scheme cenc-aes-ctr -encryption_key 76a6c65c5ea762046bd749a2e632ccbb -encryption_kid a7e61c373e219033c21091fa607bf3b8 SampleVideo_1280x720_1mb_encrypted.mp4
It is successfully getting played with ffmpeg command :
ffplay SampleVideo_1280x720_1mb_encrypted.mp4 -decryption_key 76a6c65c5ea762046bd749a2e632ccbb
But I do not want to use ffmpeg in android so I am using libeasy library which creates a local http server to make a stream of offline video and then decrypt it on the fly using Cipher. The Cipher which is provided to the LocalHttpServer for decryption process, needs the initialisation vector (iv) for CTR mode which is not externally available to us. From above command it is clear that ffmpeg doesn't require the IV for encryption or decryption to be passed but i guess internally it uses 8 byte random iv.
This is how i am creating Cipher to pass to LocalHttpServer :
final Cipher c = Cipher.getInstance("AES/CTR/NoPadding","BC); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(hexStringToBytes("76a6c65c5ea762046bd749a2e632ccbb"), "AES"), new IvParameterSpec(new byte[16]));
So my question is, what value of iv should i pass in this case to Cipher ? My basic requirement is to play a offline MPEG-CENC encrypted mp4 video stored on device. Keeping in mind my very limited knowledge of cryptography, references to any library that provides such implementation with or without any tweaks is welcomed.
-
Efficient command line to crop a video, overlay another crop from it and scale the result with ffmpeg
26 mai 2018, par WitekI need to convert many videos in such a way that I take 2 different crops from each frame of a single video, stack them one over the other and scale down the result, creating a new smaller video. I want to convert this fullHD frame (two crop areas are marked red) to this small stacked frame.
Right now I use the following code:
ffmpeg -i "video.mkv" -filter:v "crop=560:416:0:0" out1.mp4 ffmpeg -i "video.mkv" -filter:v "crop=560:384:1060:128" out2.mp4 ffmpeg -i out1.mp4 -vf "movie=out2.mp4[inner]; [in][inner] overlay=0:32,scale=280:208[out]" -c:v libx264 -preset veryfast -crf 30 result.mp4
It works but it is very inefficient and requires temporary files (out1 and out2). And the problem is I have over 100.000 of such videos (they are big and stored on a NAS and not directly on my computer's HDD). Converting all of them with a Windows batch script (for loop) will take...48 days. Can you help me to optimize the script?
-
Software genlock by matching frames in post
26 mai 2018, par Fabien BillerI have two videos
v1
andv2
which form a stereo pair. They are adjusted in a first step using audio-matching.v1
is 30 fps,v2
is 60fps.v1
andv2
are not genlocked. The goal is to genlock them by software, ie match the best one of two frames ofv2
with each frame ofv1
. I found framesync and framepack, whereas they have different goals. I thought about doubling the framerate ofv1
, but that will always leave one wrong frame, but it might even it out. Or I recordv2
with 90 fps, convertv1
to 90 fps to then convert both down to 60 fps. -
libavformat produces flv files without size while encoding
25 mai 2018, par seanr8I'm encoding a video with libavcodec and libavformat. I'm using libx264 as the codec and flv as the container. I chose flv because I wanted to be able to play and cut pieces of a video while it is encoding and flv is supposed to support this behavior.
When I attempt to cut a part (first 5 seconds) of the video with ffmpeg while it is still encoding, ffmpeg produces an error:
$# ffmpeg -i file.flv -t 00:00:05.000 -c copy test.mp4 [flv @ 0x67ecb00] Could not find codec parameters for stream 0 (Video: h264, none, 2000 kb/s): unspecified size
If I wait until the file has finished encoding and run the same ffmpeg command, there is no error. In fact, ffprobe shows the following for the video:
Video: h264 (High), yuv420p, 640x480, 2000 kb/s, 25 fps, 25 tbr, 1k tbn, 50 tbc
The file is definitely more than 5 seconds long when I attempt this.
The code I'm using to do this is based heavily on the muxing.c example from libavformat. Here's a trimmed version:
AVOutputFormat *container_format; AVFormatContext *container_format_context; AVStream *video_stream; char *path = "/home/user/temp.flv"; /* allocate the output media context */ avformat_alloc_output_context2(&container_format_context, NULL, NULL, path); container_format = container_format_context->oformat; AVCodec* codec = NULL; /* Pull codec based on name */ codec = avcodec_find_encoder_by_name("libx264"); /*create stream */ video_stream = NULL; video_stream = avformat_new_stream(container_format_context, codec); video_stream->id = container_format_context->nb_streams - 1; video_stream->codec->bit_rate = bitrate; video_stream->codec->width = 640; video_stream->codec->height = 480; video_stream->codec->gop_size = 10; video_stream->codec->qmax = 31; video_stream->codec->qmin = 2; video_stream->codec->pix_fmt = AV_PIX_FMT_YUV420P; video_stream->codec->time_base = (AVRational) { 1, 25 }; AVCodecContext *avcodec_context = video_stream->codec; if (container_format_context->oformat->flags & AVFMT_GLOBALHEADER) { avcodec_context->flags |= CODEC_FLAG_GLOBAL_HEADER; } /* open codec */ guacenc_open_avcodec(avcodec_context, codec, NULL, video_stream); /* Allocate corresponding frame */ AVFrame* frame = av_frame_alloc(); /* Copy necessary data for frame from avcodec_context */ frame->format = avcodec_context->pix_fmt; frame->width = avcodec_context->width; frame->height = avcodec_context->height; if (!(container_format->flags & AVFMT_NOFILE)) { avio_open(&container_format_context->pb, path, AVIO_FLAG_WRITE); } avformat_write_header(container_format_context, NULL);
How can I get the size information to the front of the flv file so that it can be trimmed while still encoding?
-
FFMPEG/tensorflow warping .mp4's with no observable pattern of correlation
25 mai 2018, par ThePeskyWabbitThe function that performs the processing is here:
def ffwd_video(path_in, path_out, checkpoint_dir, device_t='/gpu:0', batch_size=4): video_clip = VideoFileClip(path_in, audio=False) video_writer = ffmpeg_writer.FFMPEG_VideoWriter(path_out, video_clip.size, video_clip.fps, codec="libx264", preset="slow", bitrate="3000k", audiofile=path_in, def ffwd_video(path_in, path_out, checkpoint_dir, device_t='/gpu:0', batch_size=2):threads=None, ffmpeg_params=None) g = tf.Graph() soft_config = tf.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), g.device(device_t), \ tf.Session(config=soft_config) as sess: batch_shape = (batch_size, video_clip.size[1], video_clip.size[0], 3) print(batch_shape) img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) print(preds) saver = tf.train.Saver() if os.path.isdir(checkpoint_dir): ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise Exception("No checkpoint found...") else: saver.restore(sess, checkpoint_dir) X = np.zeros(batch_shape, dtype=np.float32) def style_and_write(count): for i in range(count, batch_size): X[i] = X[count] # Use last frame to fill X _preds = sess.run(preds, feed_dict={img_placeholder: X}) for i in range(0, count): video_writer.write_frame(np.clip(_preds[i], 0, 255).astype(np.uint8)) frame_count = 0 # The frame count that written to X for frame in video_clip.iter_frames(): X[frame_count] = frame frame_count += 1 if frame_count == batch_size: style_and_write(frame_count) frame_count = 0 if frame_count != 0: style_and_write(frame_count) video_writer.close()
I do not expect this to be a repeatable sample as it requires gigabytes of models/checkpoints/etc. to do so.
Anyways, I am converting .gif's to .mp4's using this line of code:
os.system('echo "y"| ffmpeg -i temp.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" temp.mp4')
and when I run a subsequent .mp4 through the video rendering function, this will sometimes happen:
original: https://i.imgur.com/WrBa6yw.gifv
result: https://giphy.com/gifs/dream-bot-BzqB8XILJuVTJcgJnn
It will happen on certain .mp4's 100% of the time and on others, 0% of the time. It either happens to a clip or it does not happen to that clip. It is not a case of a single clip sometimes having this problem. If it is good, it is always good but if it's bad, that clip will never work.
After this output data from when the clips are processed:
good gifs Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 728x728, q=-1--1, 30 fps, 15360 tbn, 30 tbc frame= 450 fps= 78 q=-1.0 Lsize= 7570kB time=00:00:14.93 bitrate=4152.6kbits/s Stream #0:0: Video: gif, bgra, 800x450, 25 fps, 25 tbr, 100 tbn, 100 tbc Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 728x408, q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc frame= 360 fps=198 q=-1.0 Lsize= 2568kB time=00:00:14.93 bitrate=1408.8kbits/s Stream #0:0: Video: gif, bgra, 728x408, 23.92 fps, 23.98 tbr, 100 tbn, 100 tbc Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 728x408, q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc frame= 360 fps=198 q=-1.0 Lsize= 2568kB time=00:00:14.93 bitrate=1408.8kbits/s bad gifs Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 800x450, q=-1--1, 25 fps, 12800 tbn, 25 tbc frame= 150 fps= 87 q=-1.0 Lsize= 1840kB time=00:00:05.92 bitrate=2545.8kbits/s Stream #0:0: Video: gif, bgra, 378x275, 25 fps, 25 tbr, 100 tbn, 100 tbc Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 378x274, q=-1--1, 25 fps, 12800 tbn, 25 tbc frame= 115 fps=0.0 q=-1.0 Lsize= 253kB time=00:00:04.52 bitrate= 459.4kbits/s
I have noticed that the
frame=
value is exactly 15x larger than the .gif fps value while for the bad .gifs, it is not. I believe this is the cause but do not know how to change these values. does anybody know how I can process/convert the gif to .mp4 so that theframe=
value is 15x the gif fps?